Spring AOP:被代理类的构造函数和调用自身类方法的注意点

Spring AOP是通过动态代理实现的:通过JDK的动态代理或者CGLIB动态生成子类.

如果被代理的bean是interface,会使用JDK的动态代理,这时候,bean的构造函数只会被调用一次,如果该bean没有实现任何interface,会使用cglib来动态生成子类,这时候构造函数会被调用两次,所以在bean的构造函数里尽量不要写业务相关的代码.

如果bean的某个方法被'AOP'了,及对该方法加入了方面关注:'advice',如funcA,另一个该类的方法,如funcB,调用funcA.客户端代码调用了

funcB,虽然funcB会调用funcA,但是关注点方法不会被执行.例子:

public class IntfBean implements Intf{

    public IntfBean() {
        System.out.println("IntfBean construcgtor");
    }

    @Override
    public void test1() {
        System.out.println("Intf test");      
    }
    public void test2() {
        test1();     
    }
}

public interface Intf {
    public void test1();
    public void test2();
}
切面配置:
@Pointcut("execution(* com.test.spring.aspectj.Intf.test1(..))")
    public void guardIntf() {
    }

    @Before(value = "guardIntf()")
    public void guardIntfBefore() {
        System.out.println("Guardian caught before guardIntf");
    }

    @AfterReturning("guardIntf()")
    public void guardIntfAfter() {
        System.out.println("Guardian caught after guardIntf ");
    }
调用代码:
ApplicationContext context = new ClassPathXmlApplicationContext("conf/aspectJAppcontext.xml");

        Intf intfBean = (Intf) context.getBean("IntfBean");
        try {
            intfBean.test2();
        } catch (Exception e) {
        }
运行结果:

构造函数执行一次(因为是interface,如果不是会执行两次)

关注点方法(guardIntfBefore和guardIntfAfter)不执行.如果客户端调用: intfBean.test1(),关注点方法(guardIntfBefore和guardIntfAfter)会执行.

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值