AOP-自动代理生成器-默认Advisor自动代理生成器
当有多个目标对象时,生成代理会很繁杂,为了简单采用自动代理生成器
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 注册多个目标对象 -->
<bean name = "someService" class="com.hk.spring.aop10.SomeServiceImpl"></bean>
<bean name = "someService1" class="com.hk.spring.aop10.SomeServiceImpl"></bean>
<!-- 注册通知 Advice-->
<bean name = "myAfterReturningAdvice" class="com.hk.spring.aop10.MyAfterReturningAdvice"></bean>
<!-- 注册顾问 Advisor-->
<bean name = "myAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myAfterReturningAdvice"></property>
<property name="mappedName" value="doFirst"/>
</bean>
<!-- 注册自动代理生成器 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>
测试
package com.hk.spring.aop10;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
@Test
public void test1(){
String resoure = "com/hk/spring/aop10/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resoure);
SomeService someService = (SomeService) ac.getBean("someService");
someService.doFirst();
someService.doSecond();
someService.doThree();
SomeService someService1 = (SomeService) ac.getBean("someService1");
someService1.doFirst();
someService1.doSecond();
someService1.doThree();;
}
}