标签:lis method throwable name top get 分享 前置 new
自动装配(基于顾问)
someService类
1 public class someService { 2 public void list(){ 3 System.out.println("list"); 4 } 5 public void add(){ System.out.println("add"); } 6 }
前置增强类
1 public class MyBeforeAdvice implements MethodBeforeAdvice { 2 @Override 3 public void before(Method method, Object[] args, Object target) throws Throwable { 4 System.out.println("before============="); 5 } 6 }
xml配置
1 <bean id="someService" class="cn.Day11.someService"></bean> 2 <!--增强--> 3 <bean id="myBeforeAdvice" class="cn.Day11.MyBeforeAdvice"></bean> 4 <!--顾问--> 5 <bean id="myAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 6 <property name="advice" ref="myBeforeAdvice"></property> 7 <property name="mappedNames" value="*li*"></property> 8 </bean> 9 10 <!--自动代理--> 11 <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> 12 <property name="target" ref="someService"></property> 13 <property name="interceptorNames" value="myAdvisor"></property> 14 </bean>
测试类
1 public class Test11 { 2 @Test 3 public void zyy(){ 4 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextDay11.xml"); 5 someService service=(someService)ctx.getBean("proxyService"); 6 service.list(); 7 service.add(); 8 }
测试结果
自动代理
前两个类相同
xml文件(也可以用顾问)
1 <bean id="someService" class="cn.Day12.someService"></bean> 2 <!--增强--> 3 <bean id="myBeforeAdvice" class="cn.Day12.MyBeforeAdvice"></bean> 4 <!--顾问--> 5 <bean id="myAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 6 <property name="advice" ref="myBeforeAdvice"></property> 7 <property name="mappedNames" value="*li*"></property> 8 </bean> 9 10 11 <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> 12 <property name="target" ref="someService"></property> 13 <property name="interceptorNames" value="myAdvisor"></property> 14 </bean> 15 16 <!--自动代理--> 17 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
测试类
1 public class Test12 { 2 @Test 3 public void De(){ 4 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextDay12.xml"); 5 someService service=(someService)ctx.getBean("someService"); 6 service.list(); 7 service.add(); 8 9 } 10 11 }
标签:lis method throwable name top get 分享 前置 new
原文地址:https://www.cnblogs.com/zhangxuuy/p/10012548.html