AOP 之 HelloWorld
2016-11-12 本文已影响54人
zlb
- 定义目标接口:
package com.zlb.service;
public interface IHelloWorldService {
public void sayHello();
}
- 实现目标接口
package com.zlb.service.imp;
import com.zlb.service.IHelloWorldService;
public class HelloWorldService implements IHelloWorldService{
@Override
public void sayHello() {
System.out.println("============Hello World!");
}
}
- 定义切面支持类
package com.zlb.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class HelloWorldAspect {
//前置通知
public void beforeAdvice() {
System.out.println("===========before advice");
}
}
- XML配置
<bean id="helloWorldService" class="com.zlb.service.imp.HelloWorldService"/>
<bean id="aspect" class="com.zlb.aop.HelloWorldAspect"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.zlb.service.*.*(..))"/>
<aop:aspect ref="aspect">
<aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
</aop:aspect>
</aop:config>
- 运行结果
===========before advice
============Hello World!admin
===========after finally advice