ABOUT ME

개발자입니다 :)

Today
Yesterday
Total
  • [Spring] AOP
    back end 2022. 3. 16. 00:59

    공통 부분 + 핵심 부분(횡단)을 구현하는 AOP (Aspect Oriented Programming).

    관점을 기준으로 묘듈화를 한다.

    공통 부분에서 Intercept해서 핵심 부분을 횡단한다.

     

    Spring Documentation 참고

    https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop

     

    Core Technologies

    In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

    docs.spring.io

     

    AOP 개념

     

     

    실습예제) SpringAOP/com/test02, test05

     

    1. pom.xml 에 아래 dependency 추가하기

    spring context

    spring core

    spring jdbc

    mysql

    spring aop

    aspectjweaver

    aspectjrt

     

    2. Beans.xml 을 작성한다.

    Configuration을 추가한다.

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop 
    		http://www.springframework.org/schema/aop/spring-aop-5.3.xsd
    		http://www.springframework.org/schema/context 
    		http://www.springframework.org/schema/context/spring-context-5.3.xsd">
    </beans>

     

    1/ xml 파일에서 AOP 구현하기

    핵심 부분인 classWork() 메서드가 실행되면 (expression과 같은 메서드가 실행되면),

    ref="myAdvice"를 참조해서 MyAspect 클래스를 실행한다.

    자동적으로 표현식에 맞는 메서드를 실행시키는 건 마지막 줄의 AutoProxyCreator bean이다.

    <bean id="myAdvice" class="com.test02.MyAspect"/>
    <bean id="myAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
    	<property name="advice" ref="myAdvice"></property>
    	<property name="expression" value="execution(public * *(..))"></property>
    </bean>
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

    공통 부분이 포함된 전체 부분을 작성할 땐 MethodInterceptor를 implements하고 invoke 메서드를 override한다.

    핵심 로직을 실행시키려는 부분에 아래 코드로 가로채기 한다.

    res = invocation.proceed();

    그 후에 main 클래스에서 메모리를 소멸시키기 위해 ApplicationContext를 close해준다.

     

     

    참고))

    execution 안에 들어갈 example

    참고 https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-pointcuts-examples

     

    Core Technologies

    In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

    docs.spring.io

    execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
                    throws-pattern?)

     

     

    2/ Annotation으로 AOP 구현

    Beans.xml 파일에 아래 코드를 추가해서 Annotation한다.

    <aop:aspectj-autoproxy/>

    Aspect 클래스 위에 @Aspect Annotation을 적는다.

    AOP는 공통 부분 + 핵심 부분 + 공통 부분 으로 이루어져있다.

    첫번재 공통 부분은 @Before()

    핵심 부분의 오류가 났을 때 실행할 메서드는 @AfterThrowing()

    핵심 부분이 실행된 후 실행될 공통부분은 @After()

     

    각 괄호 안에는 핵심 부분이 실행되는 시점을 넣어준다.

    @Before("execution(public void com.test03.*.*(..))")
    public void before(JoinPoint joinPoint) {
    }
    
    @AfterThrowing(pointcut = "execution(public void com.test03.*.*(..))", throwing = "e")
    public void throwing(JoinPoint joinPoint, Throwable e) {
    }
    
    @After("execution(public void com.test03.*.*(..))")
    public void after(JoinPoint joinPoint) {
    }

     

     

    3/ AOP 태그로 AOP 구현

    2번에서 Annotation을 모두 지우고,

    Beans.xml 에서 아래와 같이 Configuration 한다.

    <aop:aspectj-autoproxy/>
    <bean id="g7" class="com.test04.G7_Class"/>
    <bean id="student" class="com.test04.Student"/>
    <bean id="myAdvice" class="com.test04.MyAspect"/>
    		
    <aop:config>
    	<aop:aspect ref="myAdvice">
    		<aop:before method="before"
    			pointcut="execution(* classWork())" />
    					
    		<aop:after method="After"
    			pointcut="execution(* classWork())" />
    	
    		<aop:after-throwing method="throwing"
    			pointcut="execution(public void com.test04.*.*(..))" throwing="e" />
    	</aop:aspect>
    </aop:config>

    'back end' 카테고리의 다른 글

    [Spring] MVC  (0) 2022.03.16
    [Spring] DI Annotation  (0) 2022.03.15
    [JSP] EL/JSTL  (0) 2022.03.09
    [MVC] WebScore 프로젝트  (0) 2022.03.09
    [MVC 패턴] DB연동  (0) 2022.03.08
Designed by Tistory.