잡동사니에도 사랑을

[21.11.11] (sample01 - chapter03_SpringMaven) 본문

SPRING

[21.11.11] (sample01 - chapter03_SpringMaven)

luvforjunk 2021. 11. 11. 18:15
728x90
반응형
https://engkimbs.tistory.com/746
 

[Spring] 스프링 AOP (Spring AOP) 총정리 : 개념, 프록시 기반 AOP, @AOP

| 스프링 AOP ( Aspect Oriented Programming ) AOP는 Aspect Oriented Programming의 약자로 관점 지향 프로그래밍이라고 불린다. 관점 지향은 쉽게 말해 어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으..

engkimbs.tistory.com

 

 

////////pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>chapter03_SpringMaven</groupId>
	<artifactId>chapter03_SpringMaven</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<release>11</release>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- AOP -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.7</version>
			<!-- 
			error : 'ProceedingJoinPoint cannot be resolved to a type'
			scope는 이 라이브러리를 언제 사용할 것인지를 정하는 태그이다. <scope>를 주석 걸어라
			<scope>runtime</scope>
			 -->
			 <scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.3.5</version>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.20</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>

 

 

 

////////springConfiguration.java

package spring.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import sample01.LoggingAdvice;
import sample01.MessageBeanImpl;

// 일반 java 파일이 아닌 bean을 설정하는 xml파일로 취급한다.
// 때문에, @Configuration을 써줘야 한다. 그래야 Bean이 먹힌다.
@Configuration
@EnableAspectJAutoProxy // aop를 지우게 되면 @EnableAspectJAutoProxy를 써주면 된다
public class SpringConfiguration {
	
	@Bean
	public MessageBeanImpl messageBeanImpl() {
		return new MessageBeanImpl();
	}
	
	@Bean
	public LoggingAdvice loggingAdvice() {
		return new LoggingAdvice();
	}
}

 

 

 

////////MessageBean.java

package sample01;

public interface MessageBean {
	public void showPrintBefore();
	public void viewPrintBefore();
	
	public void showPrintAfter();
	public void viewPrintAfter();
	
	// 리턴값 잡아주기
	public String showPrint();
	
	public void viewPrint();
	public void display();

}

 

 

 

////////MessageBeanImpl.java

package sample01;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

// Target(삽입되는 코드를 받는 곳, 타켓클래스)
//@Component
	public class MessageBeanImpl implements MessageBean {
	private String str;

	@Autowired // 자동으로 setter 처리
	public void setStr(@Value("Have a nice day!!") String str) {
		this.str = str;
	}

	@Override
	public void showPrintBefore() {
		System.out.println("showPrintBefore 메세지 = " + str); // 핵심(관심사항)

	}

	@Override
	public void viewPrintBefore() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("viewPrintBefore 메세지 = " + str);
	}

	@Override
	public void showPrintAfter() {
		System.out.println("showPrintAfter 메세지 = " + str);

	}

	@Override
	public void viewPrintAfter() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("viewPrintAfter 메세지 = " + str);

	}

	@Override
	public String showPrint() {
		System.out.println("showPrint 메세지 = " + str);
		return "오늘 하늘이 참 예뻐요 석탄을 안때나봐";

	}

	@Override
	public void viewPrint() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("viewPrint 메세지 = " + str);

	}

	@Override
	public void display() {
		System.out.println("display 메세지 = " + str);

	}
}

 

 

 

////////LoggingAdvice.java

package sample01;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.util.StopWatch;

// 공통관심사항(부가기능)
@Aspect // 공통모듈
//@Component
public class LoggingAdvice {
	
	@Before("execution(public void sample01.MessageBeanImpl.*Before())")
	// 위의 메소드가 실행될 때 전에 끼어들 거다
	public void beforeTrace() {
		System.out.println("before trace...");
	}

	@After("execution(public * *.*.*After(..))")
	public void afterTrace() {
		System.out.println("after trace...");
	}

	@Around("execution(public * sample01.*.*Print())")
	public void trace(ProceedingJoinPoint joinPoint) throws Throwable {
		//System.out.println("앞");// 삽입
		String methodName = joinPoint.getSignature().toShortString();
		// 핵심 코드를 부르기 전에 나를 부른 아이가 누구인지 알려줘
		System.out.println("메소드 = " + methodName);
		
		StopWatch sw = new StopWatch();
		sw.start(methodName);
		
		Object ob = joinPoint.proceed(); // 핵심사항 호출
		System.out.println("결과 = " + ob);
		
		sw.stop();
		System.out.println("처리시간 = " + sw.getTotalTimeMillis()/1000+"초"); 
		// 처리하는 속도를 1000분의 1초로
		
		//System.out.println("뒤");// 삽입
	}
}

 

 

 

////////HelloSpring.java

package sample01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloSpring {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("acQuickStart.xml");
		MessageBean messageBean = (MessageBean) context.getBean("messageBeanImpl"); // id값
		
//		messageBean.showPrintBefore();
//		System.out.println();
//		messageBean.viewPrintBefore();
//		System.out.println();
		
//		messageBean.showPrintAfter();
//		System.out.println();
//		messageBean.viewPrintAfter();
//		System.out.println();
		
		messageBean.showPrint();
		System.out.println();
		messageBean.viewPrint();
		System.out.println();
		
		messageBean.display();
	}

}

 

 

 

////////acQuickStart.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
		
	<context:component-scan base-package="sample01" />	
	<context:component-scan base-package="spring.conf" /> 

	<!-- AOP -->
	<!-- <aop:aspectj-autoproxy /> -->
</beans>

 

728x90
반응형