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
阅读全文
0 0
- Spring AOP之增强(aspectj)
- Spring-AOP @AspectJ进阶之增强织入的顺序
- Spring AOP之AspectJ
- Spring AOP之AspectJ
- Spring Aop入门之Aspectj
- Spring aop之使用AspectJ
- AOP框架之:Spring AOP和AspectJ
- AOP框架之:Spring AOP和AspectJ
- AOP框架之:Spring AOP和AspectJ
- 七.Spring AOP 之增强
- Spring AOP之增强介绍
- Spring Aop入门之Aspectj 进阶1
- Spring Aop实例之AspectJ注解配置
- Spring 之AOP AspectJ切入点语法详解
- Spring-AOP之aspectj注解方式
- Spring Aop实例之AspectJ注解配置
- Spring 之AOP AspectJ切入点语法详解
- Spring 之AOP AspectJ切入点语法详解
- 序列化和读取文件内容
- spring cloud微服务分布式云架构
- error C2226: 语法错误 : 意外的“HFONT”类型
- 中山大学广州校区东校园——学生医保报销(个例)
- eclipse 关闭updating indexes
- Spring AOP之增强(aspectj)
- 【智能零售】互联网第一大品类,服装新零售全新变革
- 【人工智能】史上最完整的人工智能书单大全,学习AI的请收藏好
- OpenCV_海康威视SDK_VS2012配置及实时预览(非海康(其他)网络摄像头IPC)二次开发
- 程序员所说的「轮子」是什么东西?
- 工作中常用 Git 命令的总结
- pynq硬件编译 /usr/bin/ld: cannot find -lsds_lib
- Backpropagation 算法的推导与直观图解
- idea如何快速实现抽象方法