Spring 动态代理(五)
来源:互联网 发布:淘宝演唱会门票可靠吗 编辑:程序博客网 时间:2024/05/29 18:03
BeanTypeAutoProxyCreator
自定义BeanTypeAutoProxyCreator,根据bean的类型来代理bean。
接口
public interface People { public void eat();}
实现类
public class Student implements People { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void eat() { System.out.println(this.getName() + " is eating"); } @Override public String toString() { return "Student{" + "name='" + name + '\'' + '}'; }}
方法拦截
public class MyMethodInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("--------before method interceptor---------"); Object object = methodInvocation.proceed(); System.out.println("----------after method interceptor----------"); return object; }}
自定义BeanTypeAutoProxyCreator
public class BeanTypeAutoProxyCreator extends AbstractAutoProxyCreator { private List<String> beanTypes; public List<String> getBeanTypes() { return beanTypes; } public void setBeanTypes(List<String> beanTypes) { this.beanTypes = beanTypes; } @Override protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource) throws BeansException { return isMatch(beanClass) ? PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS : DO_NOT_PROXY; } private boolean isMatch(Class<?> clazz) { try { for (String beanType : beanTypes) { Class cl = Class.forName(beanType); if (cl.isAssignableFrom(clazz)) { return true; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } return false; }}
配置文件
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.lilongjiu.study.spring.aop.aposrc"></context:component-scan> <bean id="student" class="com.lilongjiu.study.spring.aop.aposrc.Student"> <property name="name" value="zhangsan"></property> </bean> <bean id="myMethodInterceptor" class="com.lilongjiu.study.spring.aop.aposrc.MyMethodInterceptor"></bean> <bean id="myBeanTypeAutoProxyCreator" class="com.lilongjiu.study.spring.aop.aposrc.BeanTypeAutoProxyCreator"> <property name="beanTypes"> <list> <value>com.lilongjiu.study.spring.aop.aposrc.People</value> </list> </property> <property name="interceptorNames"> <list> <value>myMethodInterceptor</value> </list> </property> </bean></beans>
main方法
public class Demo { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); People people = (People) applicationContext.getBean("student"); people.eat(); }}
输出
--------before method interceptor---------zhangsan is eating----------after method interceptor----------
Spring 动态代理(一) - 代理模式
Spring 动态代理(二) - ProxyFactoryBean
Spring 动态代理(三) - BeanNameAutoProxyCreator
Spring 动态代理(四)- 动态代理核心类 - ProxyCreatorSupport
Spring 动态代理(五) - 自定义BeanTypeAutoProxyCreator
阅读全文
0 0
- Spring 动态代理(五)
- 动态代理(五)
- Spring动态代理(一)
- Spring 动态代理(一)
- Spring 动态代理(二)
- Spring 动态代理(三)
- (五)sping之CGLIB动态代理
- Spring的代理模式(静态代理和动态代理)
- Spring的代理模式(静态代理和动态代理)
- Spring 动态代理(四)- 动态代理核心类
- 代理模式(五)JDK动态代理深入分析
- 代理模式(五)JDK动态代理深入分析
- 代理模式(五)JDK动态代理深入分析
- 【spring】静态代理&&动态代理
- spring静态代理 动态代理
- spring静态代理 动态代理
- spring静态代理 动态代理
- spring静态代理 动态代理
- JNI(一) 认识JNI 上(jni.h里都有啥?)
- Springboot配置使用ssl,使用https
- 程序员你为什么这么累?
- SSH与SSM学习之hibernate21——离线查询
- 简单的maven搭建
- Spring 动态代理(五)
- 源码解析 ReentrantLock
- JDK1.8中接口
- ajax异步请求post方式
- 设计模式-单例模式
- Sublime Text 3 全程详细图文原创教程(持续更新中。。。)
- Maximum Flow 规律题 2017 ACM-ICPC 亚洲区(西安赛区)网络赛
- 8. 用while循环计算打印2到100的偶数和(三种方法)
- 动态库与静态库