개발관련/Spring 2015. 11. 30. 15:13

pom.xml은 바뀐게 없다. 저번에 설명을 못한 부분이 있는데 weaving이라는 것이다. 이부분은 

http://najuung.tistory.com/65 이분의 사이트를 참조하라 


바로 소스로 들어가자. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
<?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: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/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.gno.sample"></context:component-scan>
    
    <aop:aspectj-autoproxy />
    
 
    <bean id="simpleCalcService" class="com.gno.sample.service.SimpleCalcServiceImpl"></bean>
    <bean id="calcDao" class="com.gno.sample.dao.CalcDaoImpl"></bean>
 
    <bean id="loggingAspect" class="com.gno.sample.aop.SimpleAopAspect"></bean>
    
</beans>
 
cs


기존 aop:config에서 scan과 annotation-config, autoproxy 가 추가 되었다. 그리고 기존 pointcut과 expression등은 사라졌다.  simpleAppAspect 클래스를 살펴보자. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
package com.gno.sample.aop;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
 
@Aspect
public class SimpleAopAspect {
 
    @Before("execution(* com.gno.sample.dao.CalcDAO.*(..)) ")
    public void before(JoinPoint joinPoint) {
        System.out.println("before!");
        Signature signature = joinPoint.getSignature();
        Object target = joinPoint.getTarget();
        Object[] args = joinPoint.getArgs();
 
        System.out.println("Signature : " + signature.toString());
        System.out.println("target : " + target.toString());
        System.out.println("name : " + signature.getName());
        System.out.println("longName : " + signature.toLongString());
        System.out.println("shortName : " + signature.toShortString());
    }
 
    @After("execution(* com.gno.sample.dao.CalcDAO.*(..))")
    public void after(JoinPoint joinPoint) {
        System.out.println("after!");
        Signature signature = joinPoint.getSignature();
        Object target = joinPoint.getTarget();
        Object[] args = joinPoint.getArgs();
 
        System.out.println("Signature : " + signature.toString());
        System.out.println("target : " + target.toString());
        System.out.println("name : " + signature.getName());
        System.out.println("longName : " + signature.toLongString());
        System.out.println("shortName : " + signature.toShortString());
    }
}
cs

@Aspect 라는 annotation으로 선언을 하면 기존 config와 같은 역활을 한다. 이후 @Before 등로 pointcut등을 지정한다. 나머진 같다. 

소스는 https://github.com/zest133/SpringAOPSample/ 

후 왠지 빈약하네 =_=;; 사실 머 별거 없지


posted by 제스트
: