-
Spring AOP 설정방법 - @annotationSpring/Spring 기본 지식 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 붙이기
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을 이용하여 설정하는게 직관적이라 보기 좋은거 같다...
반응형'Spring > Spring 기본 지식' 카테고리의 다른 글
Spring AOP 설정방법 - @annotation 두 번째(백기선님) (0) 2020.03.30 Spring AOP 설정방법 - execution (0) 2020.03.30 JDBC 사용 - 커넥션 풀 (0) 2020.03.23 Spring postgresql mybatis jdbc 기본 연결테스트 (0) 2020.03.23 서블릿 컨테이너와 스프링 컨테이너 (0) 2020.03.19