Spring/Spring 기본 지식

Spring AOP 설정방법 - @annotation

레알윙 2020. 3. 25. 10:14
반응형

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에서 @anntation 붙이기

Controller

 

4. @annotation interface 만들기

public @interface ParameterCheck {
}

 

5. @annotation 구현체 만들기

@Aspect
@Component
public class ParameterCheckAspect {
	
	@Pointcut("@annotation(com.test.test1.api.aop.aspect.ParameterCheck)")
	public void parameterCheck() {
	}

	@Before("parameterCheck()")
	public void checkParameter() throws Throwable {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
				.getRequest();
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
				.getResponse();
		System.out.println("=== AOP TEST ===");
	}

}

 

spring AOP설정은 여러가지 방법이 있는데 @annocation을 이용하여 설정하는게 직관적이라 보기 좋은거 같다...

 

반응형