Spring AOP不适用于另一个方法中的方法调用

ABC.java中定义了两种方法


public void method1(){

   .........

   method2();

  ...........

}



public void method2(){

  ...............

  ...............  

}

我想有AOP上的呼叫方法2。所以,我创建了一个类,AOPLogger.java,具有在方法提供方面功能的checkAccess

在配置文件中,我不喜欢的东西下面


<bean id="advice" class="p.AOPLogger" />

<aop:config>

  <aop:pointcut id="abc" expression="execution(*p.ABC.method2(..))" />

  <aop:aspect id="service" ref="advice">

    <aop:before pointcut-ref="abc" method="checkAccess" />          

  </aop:aspect>

</aop:config>

但是,当调用我的method2时,不会调用AOP功能,即不会调用AOPLogger类的checkAccess方法。


我有什么想念的吗?


白猪掌柜的
浏览 1054回答 3
3回答

尚方宝剑之说

我有同样的问题,我克服了通过实现Spring的ApplicationContextAware,BeanNameAware并实施相应的方法如下。class ABC implements ApplicationContextAware,BeanNameAware{&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void setApplicationContext(ApplicationContext ac) throws BeansException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; applicationContext=ac;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void setBeanName(String beanName) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.beanName=beanName;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; private ApplicationContext applicationContext;&nbsp; &nbsp; &nbsp; private String beanName;}然后我在调用同一类的方法时将其替换this.为 ((ABC) applicationContext.getBean(beanName)).。这样可以确保对同一类方法的调用仅通过代理进行。因此method1()更改为&nbsp;public void method1(){&nbsp; &nbsp; .........&nbsp; &nbsp; ((ABC) applicationContext.getBean(beanName)).method2();&nbsp; &nbsp; ...........&nbsp; }希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP