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