Spring AOP之增强(aspectj)

来源:互联网 发布:数据库删除记录 编辑:程序博客网 时间:2024/05/21 08:19
按照增加在目标类方法连接点的位置可以将增强划分为以下五类:
  • 前置增强   (org.springframework.aop.BeforeAdvice)   表示在目标方法执行前来实施增强
  • 后置增强   (org.springframework.aop.AfterReturningAdvice)   表示在目标方法执行后来实施增强
  • 环绕增强   (org.aopalliance.intercept.MethodInterceptor)   表示在目标方法执行前后同时实施增强
  • 异常抛出增强   (org.springframework.aop.ThrowsAdvice)   表示在目标方法抛出异常后来实施增强
  • 引介增强   (org.springframework.aop.introductioninterceptor)   表示在目标类中添加一些新的方法和属性
其中,引介增强是一种特殊的增强。他可以在目标中添加属性和方法,通过拦截定义一个接口,让目标代理实现这个接口。他的连接点是级别的,而前面的几种则是方法级别的。

其中,环绕增强是AOP联盟定义的接口,其他四种增强接口则是Spring定义的接口

package Before;public interface Waiter {public void greetTo(String name);public void serveTo(String name);}

package Before;public class NativeWaiter implements Waiter {@Overridepublic void greetTo(String name) {System.out.println("greet to " + name + "...");}@Overridepublic void serveTo(String name) {System.out.println("serving to " + name + "...");}}
package Before;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class GreetingBeforeAdvice implements MethodBeforeAdvice{@Overridepublic void before(Method method,Object[] args,Object obj) {String clientName = (String)args[0];System.out.println("How are you!Mr." + clientName);}}
<?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:p="http://www.springframework.org/schema/p"      xmlns:aop="http://www.springframework.org/schema/aop"      xmlns:context="http://www.springframework.org/schema/context"      xsi:schemaLocation="       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.0.xsd       http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <context:annotation-config />        <bean id="advice" class="Before.GreetingBeforeAdvice"></bean>    <bean id="target" class="Before.NativeWaiter"></bean>    <bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"    p:proxyInterfaces="Before.Waiter"    p:target-ref="target"    p:interceptorNames="advice"     /></beans>
package Before;import org.springframework.aop.BeforeAdvice;import org.springframework.aop.framework.ProxyFactory;import org.springframework.aop.framework.ProxyFactoryBean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAdvice {public static void main(String[] args) {//通过代码实现aop aspectj//Waiter target = new NativeWaiter();//BeforeAdvice advice = new GreetingBeforeAdvice();////ProxyFactory pf = new ProxyFactory();//pf.setTarget(target);//pf.addAdvice(advice);////Waiter proxy = (Waiter)pf.getProxy();//proxy.greetTo("aaaa");//proxy.serveTo("bbbb");////通过xmlApplicationContext ctx = new ClassPathXmlApplicationContext("conf.xml");Waiter waiter = (Waiter)ctx.getBean("waiter");waiter.greetTo("ccc");waiter.serveTo("dddd");}}
注意需要使用AspectJ需要引入一些jar包:

aopalliance.jar

aspectjrt.jar

aspectjtools.jar

aspectjweaver.jar

org.aspectj.matcher.jar

可以根据需要引入上面的部分包,不一定全部需要引入,然后就可以编译通过了,

如果还缺其他的包,可以百度查,然后下载下来就可以了。

资源下载地址:http://download.csdn.net/download/a1317338022/10130277










原创粉丝点击