develog

[Spring] Spring3 AOP 설정, xml or java 본문

Dev/Spring

[Spring] Spring3 AOP 설정, xml or java

냐옴 2015. 6. 4. 15:10

<필요 jar>

pom.xml

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>3.1.1.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>3.1.1.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>3.1.1.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>3.1.1.RELEASE</version>

</dependency>


<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.6.8</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjrt</artifactId>

<version>1.6.8</version>

</dependency>


<dependency>

<groupId>cglib</groupId>

<artifactId>cglib</artifactId>

<version>2.2</version>

</dependency>



<방법1>

spring-config.xml

<context:component-scan base-package="com.my" />


<bean id="myAspect" class="com.LoggingAspect" />

<aop:config>

<aop:pointcut id="myPointcut" expression="execution(* *..*.*(..))" />

<aop:aspect ref="myAspect">

<aop:before method="before" pointcut-ref="myPointcut" />

<aop:after-returning method="afterReturning" pointcut-ref="myPointcut"  returning="retVal" />

</aop:aspect>

</aop:config>


LoggingAspect.java

package com.my;


import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.Signature;


public class LoggingAspect {


public void before(JoinPoint joinPoint) {

System.out.println();

System.out.println(">> before()");

}

public void afterReturning(JoinPoint joinPoint, Object retVal) {

System.out.println();

System.out.println(">> afterReturning()");

}

}



<방법2>

LoggingAspect.java

package com.my;


import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.Signature;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.EnableAspectJAutoProxy;


@Configuration

@EnableAspectJAutoProxy

@Aspect

public class LoggingAspect {


@Before("execution(* *..*.*(..))")

public void before(JoinPoint joinPoint) {

System.out.println();

System.out.println(">> before()");

}

@AfterReturning("execution(* *..*.*(..))")

public void afterReturning(JoinPoint joinPoint, Object retVal) {

System.out.println();

System.out.println(">> afterReturning()");

}

}


'Dev > Spring' 카테고리의 다른 글

[Spring] beans.xml 설정1, 기본  (0) 2015.08.28
[Spring] beans.xml  (0) 2015.06.04
[Spring] static method injection  (0) 2014.11.11
[Spring] Spring 2.5, Properties #2  (0) 2014.11.11
[Spring] Spring 2.5, Properties  (0) 2014.11.10
Comments