前面所讲的代理,都必须手动配置。下面来介绍一下自动代理。
前面的案例中,每个代理都是通过ProxyFactoryBean织入切面代理,在实际开发中,非常多的Bean每个都配置ProxyFactoryBean开发维护量巨大。自动创建代理(*****基于后处理Bean.在Bean创建的过程中完成的增强.生成Bean就是代理。)
(1)BeanNameAutoProxyCreator根据Bean名称创建代理
(2)DefaultAdvisorAutoProxyCreator根据Advisor本身包含信息创建代理
*AnnotationAwareAspectJAutoProxyCreator 基于Bean中的AspectJ 注解进行自动代理
之前我们手动创建代理的时候,注意到一点,都是先创建被代理对象,然后在创建代理的时候,传入该被代理对象。
而下面要学习的两种自动代理,是基于后处理bean,在类创建的过程中完成增强,生成的bean,就是代理!
1、如下所示,先清理配置文件中其他部分,留下最简洁的四个bean配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 定义目标对象 -->
<bean id="orderDao" class="com.helloAOP.regexp.OrderDao"></bean>
<bean id="customerDao" class="com.helloAOP.regexp.CustomerDaoImpl"></bean>
<!-- 定义增强 -->
<bean id="aroundAdvice" class="com.helloAOP.regexp.MyAroundAdvice"></bean>
<bean id="beforeAdvice" class="com.helloAOP.regexp.MyBeforeAdvice"></bean>
</beans>
2、下面使用自动代理。如上所述有两种方式,基于名称、基于AspectJ注解信息。
2.1、基于名称
<!-- 自动代理:按名称代理 基于后处理bean,后处理bean不需要配置ID-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Dao"/>
<property name="interceptorNames" value="beforeAdvice"/>
</bean>
编写测试类:
package com.helloAOP.autoProxy;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.helloAOP.regexp.OrderDao;
public class TestDemo2 {
@Test
public void testDemo(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
OrderDao orderDao = (OrderDao)applicationContext.getBean("orderDao");
orderDao.add();
orderDao.update();
orderDao.delete();
orderDao.find();
applicationContext.close();
}
}
运行测试:
成功。
2.2、基于AspectJ注解信息,根据切面中定义的信息来生成代理。
修改配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 定义目标对象 -->
<bean id="orderDao" class="com.helloAOP.regexp.OrderDao"></bean>
<bean id="customerDao" class="com.helloAOP.regexp.CustomerDaoImpl"></bean>
<!-- 定义增强 -->
<bean id="aroundAdvice" class="com.helloAOP.regexp.MyAroundAdvice"></bean>
<bean id="beforeAdvice" class="com.helloAOP.regexp.MyBeforeAdvice"></bean>
<!-- 定义一个带有切点的切面 -->
<bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern" value=".*add.*"></property>
<property name="advice" ref="aroundAdvice"></property>
</bean>
<!-- 自动代理:-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>
编写测试类:
package com.helloAOP.autoProxy;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.helloAOP.regexp.OrderDao;
public class TestDemo3 {
@Test
public void testDemo(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");
OrderDao orderDao = (OrderDao)applicationContext.getBean("orderDao");
orderDao.add();
orderDao.delete();
applicationContext.close();
}
}
运行测试:
成功。