Spring/Spring 기본 지식

Spring AOP 설정방법 - execution

레알윙 2020. 3. 30. 22:30
반응형

1. pom.xml 추가

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

 

2. servelt-context에 설정 추가

<context:annotation-config />
	<context:component-scan base-package="com.victolee.aoptest">
        <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Repository" />
        <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Service" />
        <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Component" />
	</context:component-scan>
<aop:aspectj-autoproxy />

 

3. 코드 설정

@Controller
public class HomeController {
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) throws Exception {
		System.out.println("메소드 안이여");
		return "home";
	}
}
@Component
@Aspect
public class PerfAspect {
	
	// logPerf 메소드 즉 어드바이스를 어떻게 정의할 것인가
	// @Around poinitcut 설정
	
	@SuppressWarnings("unused")
	@Around("execution(* kr.co..*.HomeController.*(..))")
	public Object logPerf(ProceedingJoinPoint pjp) throws Throwable{
		Long begin = System.currentTimeMillis();
		System.out.println("메소드 호출 전이여");
		Object raVal = pjp.proceed();
		System.out.println("메소드 호출 후여");
		return raVal;
	}
}
반응형