Spring 顾问
Posted <烟花易冷>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 顾问相关的知识,希望对你有一定的参考价值。
1.名称匹配方法切入点顾问
接口:ISomeService
public interface ISomeService { public void doSome(); public void doSecont(); }
类
public class MyBeforeAdvise implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==========log=========="); } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } public void doSecont() { System.out.println("=================Secont================"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!--前置通知 名称匹配方法切入点顾问--> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring08aop_Advice.SomeService"></bean> <!--02.前置增强(通知)--> <bean id="beforeAdvice" class="cn.happy.spring08aop_Advice.MyBeforeAdvise"></bean> <!--02.前置增强(顾问)--> <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="mappedNames" value="beforeAdvisor"></property> </bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property> <property name="proxyTargetClass" value="true"></property> </bean> </beans>
单测
//1.前置通知 名称匹配方法切入点顾问 @Test public void test01(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext05_aop05_Advice.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); service.doSecont(); }
2.正则表达式 匹配方法切入点顾问
接口:ISomeService
public interface ISomeService { public void doSome(); public void doSecont(); }
类
public class MyBeforeAdvise implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==========log=========="); } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } public void doSecont() { System.out.println("++===================Secont====================++"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!--正则表达式 匹配方法切入点顾问--> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring09aop_AdviceZenz.SomeService"></bean> <!--02.前置增强(通知)--> <bean id="beforeAdvice" class="cn.happy.spring09aop_AdviceZenz.MyBeforeAdvise"></bean> <!--02.前置增强(顾问)--> <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="pattern" value=".*d.*"></property> </bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvisor"></property> </bean> </beans>
单测
//2.正则表达式 匹配方法切入点顾问 @Test public void test02(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext06_aop06_Zenz.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); service.doSecont(); }
以上是关于Spring 顾问的主要内容,如果未能解决你的问题,请参考以下文章
SSM-Spring-12:Spring中NameMatchMethodPointcutAdvisor名称匹配方法切入点顾问