Spring三种实现自动代理
来源:互联网 发布:淘宝双十一抢手机 编辑:程序博客网 时间:2024/06/05 02:13
Spring三种实现自动代理
1.基于bean配置名规则的自动代理创建器:允许为一组特定配置名的bean自动创建代理实例的代理创建器,实现类为:BeanNameAutoProxyCreator
2.基于Advisor匹配机制的自动代理创建器,实现类为:DefaultAdvisorAutoProxyCreator
3.基于bean中的AspectJ注解标签的自动代理创建器
第一种:
Waiter类:
package aop.beannameauto;public class Waiter {public void greet(String name) {System.out.println("Waiter.greet()"+name);}}
前置方法增强:
package aop.beannameauto;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class GreetB implements MethodBeforeAdvice{@Overridepublic void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {// TODO Auto-generated method stubString name= (String) arg1[0];System.out.println("GreetB.before()"+name);}}
XML配置文件:
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><bean id="waiter" class="aop.beannameauto.Waiter" /><bean id="greetingAdvice" class="aop.beannameauto.GreetB" /> <beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"p:beanNames="*er" p:interceptorNames="greetingAdvice"p:optimize="true"/> </beans>
BeanNameAutoProxyCreator有一个beanname属性,允许用户指定一组需要自动代理的bean名称
测试类:
package aop.beannameauto;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanTest {@Testpublic void test() {ApplicationContext context = new ClassPathXmlApplicationContext("aop/beannameauto/beans.xml");Waiter waiter = (Waiter) context.getBean("waiter");waiter.greet("11");}}
第二种:
Waiter类和增强类同上
配置文件:
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><bean id="waiter" class="aop.defaultauto.Waiter" /><bean id="greetingAdvice" class="aop.defaultauto.GreetB" /><bean id="regexpAdvisor"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"p:patterns=".*greet.*" p:advice-ref="greetingAdvice" /><beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /></beans>
测试类:
package aop.defaultauto;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanTest {@Testpublic void test() {ApplicationContext context = new ClassPathXmlApplicationContext("aop/defaultauto/beans.xml");Waiter waiter = (Waiter) context.getBean("waiter");waiter.greet("11");}}
第三种:
Waiter类同上
增强类:
package aop.aspect;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class GreetB {@Before("execution(* greet(..))")public void before() {System.out.println("GreetB.before()");}}
在类处标注了@AspectJ注解,第三方处理程序就可以通过类是否拥有@AspectJ注解判断是否为一个切面
其次,在方法定义出标注了@Before注解,并为该注解提供了成员值"execution(* greet(..))",提供了两个信息:@Before表示前置增强,成员值是一个@AspectJ切点表达式,意思在目标类的greet方法上织入增强
测试类:
package aop.aspect;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;public class AspectTest {@Testpublic void test() {Waiter waiter = new Waiter();AspectJProxyFactory factory = new AspectJProxyFactory();factory.setTarget(waiter);factory.addAspect(GreetB.class);Waiter waiter2 = factory.getProxy();waiter2.greet("11");}}
阅读全文
0 0
- Spring三种实现自动代理
- 【框架】[Spring]AOP拦截-三种方式实现自动代理
- SPRING源码学习之路(三)——<aop:config>自动代理的实现
- 使用BeanNameAutoProxyCreator实现spring的自动代理
- 使用DefaultAdvisorAutoProxyCreator实现spring的自动代理
- 使用BeanNameAutoProxyCreator实现spring的自动代理
- 使用DefaultAdvisorAutoProxyCreator实现spring的自动代理
- 使用DefaultAdvisorAutoProxyCreator实现spring的自动代理
- Spring, 基于自动代理实现aop
- 使用BeanNameAutoProxyCreator实现spring的自动代理
- Spring三种代理方式
- spring事务管理(三)--用BeanNameAutoProxyCreator自动创建事务代理
- Spring AOP使用配置介绍(三):自动创建代理
- JDK、CGLIB、Sring 三种实现代理的区别(三)Spring的ProxyFactory
- spring自动代理
- spring 自动代理分析
- Spring自动代理
- spring 之自动代理
- Sepic变换器的基本原理
- bzoj 4810: [Ynoi2017]由乃的玉米田 (莫队+bitset)
- 601. Human Traffic of Stadium
- [教程]centos6.3从python2.6升级到python2.7
- 通过free命令获得系统剩余的内存大小
- Spring三种实现自动代理
- 微信小程序外部字体的导入
- 215. Kth Largest Element in an Array
- JQuery 操作AJax
- 华为机试——字符串最后一个单词的长度
- JSONSerializer
- Spring的事件和监听器-同步与异步
- MTK 3G mode配置
- php的常用数组函数举例