Spring 顾问 自动代理
其他
2018-11-27 02:30:47
阅读次数: 0
- 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.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();
- }
转载自blog.csdn.net/qq_36074218/article/details/76438251