Clgb动态代理

public interface ISomeService {
    public void say1();
    public void say2();
    public void say3();
    public void say4();
}
接口
 */
public class MyAfterAdvice implements AfterReturningAdvice {

    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("后置增强");
    }
}
增强
public class SomeServiceImpl implements ISomeService {

    public void say1() {
        System.out.println("1");
    }

    public void say2() {
        System.out.println("1");
    }

    public void say3() {
        System.out.println("1");
    }

    public void say4() {
        System.out.println("1");
    }
}
接口实现
<?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"
       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

">
<bean id="service" class="cn.happy.day12aop03.SomeServiceImpl"></bean>
<bean id="afterAdvice" class="cn.happy.day12aop03.MyAfterAdvice"></bean>
  <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
      <property name="advice" ref="afterAdvice"></property>
      <property name="mappedNames" value="*2*"></property>
  </bean>
    <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="beforeAdvisor"></property>
    </bean>
</beans>
配置
public class Test20171012 {

   /* @Test
    public void test03(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext10aop01.xml");
        ISomeService service = (ISomeService) context.getBean("serviceProxy");
       service.say();
    }*/
   /* @Test
    public void test02(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext11aop02.xml");
        ISomeService service = (ISomeService) context.getBean("serviceProxy");
        service.say();
    }*/
    @Test
    public void test02(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext12aop03.xml");
        ISomeService service = (ISomeService) context.getBean("serviceProxy");
        service.say1();
        service.say2();
        service.say3();
        service.say4();

    }

}
测试类

 

posted @ 2018-02-28 12:29  李俊lj  阅读(161)  评论(0编辑  收藏  举报