public interface IDog1 {
public void dog();
public void run();
}
public class MyAfter implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("=====after===");
}
}
public class Dog1 implements IDog1 {
public void dog(){
System.out.println("===测试顾问==1dog()");
}
public void run() {
System.out.println("====测试顾问===2run()");
}
}
public class MyBefore1 implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("======before====");
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<bean id="dog" class="cn.springGuWenZiDongDaiLi.Dog1"></bean>
<bean id="before" class="cn.springGuWenZiDongDaiLi.MyBefore1"></bean>
<bean id="aftet" class="cn.springGuWenZiDongDaiLi.MyAfter"></bean>
<!--正则 表达式匹配-->
<!--<bean id="GuWen" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="before"></property>
<property name="patterns" value=".*do.*"></property>
</bean>
<bean id="GuWen2" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="aftet"></property>
<property name="patterns" value=".*do.*"></property>
</bean>-->
<!--自动代理 缺点只能代理 顾问 模式-->
<!--<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>-->
<!--自动代理 代理顾问 也可以代理 通知-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="dog"></property>
<property name="interceptorNames" value="aftet"></property>
</bean>
</beans>
//顾问 自动 代理
@Test
public void aVoid2(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext08GuWenZiDongDaiLi.xml");
IDog1 dog = (IDog1)ctx.getBean("dog");
dog.dog();
System.out.println();
dog.run();
}