读书人

spring2 AOP 配备

发布时间: 2012-10-06 17:34:01 作者: rapoo

spring2 AOP 配置
xml方式:
<context:annotation-config />
<context:component-scan base-package="com.annotation"/>
<bean id="logInterceptor" ref="logInterceptor">
<aop:before method="before" pointcut="execution(public * com.service..*.add(..))" />
</aop:aspect>
</aop:config>

annotation方式:
<context:annotation-config />
<context:component-scan base-package="com.aop"/>
<aop:aspectj-autoproxy />

@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.service..*.add(..))")
public void myMethod(){};

@Before("myMethod()")
public void before() {
System.out.println("method before");
}

@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}

}

读书人网 >软件架构设计

热点推荐