Spring AOP - 切入点未触发

发布于 2024-09-11 23:25:03 字数 1803 浏览 4 评论 0原文

我刚刚开始在我的项目中使用 Spring AOP,并且在使 Spring AOP 正常工作方面遇到一些问题。

我有两个对象,TransportImpl 和 SessionImpl,我想通过 AOP 对其进行分析。两个对象(bean)都是通过 Spring 初始化的。两个 bean 都是业务的实现 接口(传输和会话)。我可以让应用于 TransportImpl bean 的方面正常工作,但应用于 SessionImpl 的方面却不会触发。我可以确认 “mySessionMonitor”Aspect 由 Spring 初始化,并且 SessionImpl 对象也被初始化,没有任何异常或错误。

我已经将切入点和方面简化为最基本的形式。我期望下面描述的 PointCut sessionOperation 当初始化 SessionImpl bean 并调用 init 方法初始化时触发。但这永远不会发生。这里可能出了什么问题?

来自配置文件:

<bean id="MyTransport" class="my.app.transport.TransportImpl"   scope="singleton" />                
<bean id="MySession" class="my.app.session.SessionImpl" init-method="initialise" scope="singleton" />

<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="myTransportMonitor" />
    <aop:include name="mySessionMonitor" />
</aop:aspectj-autoproxy>

<bean id="myTransportMonitor" class="my.app.aspects.TransportMonitoringAspect"/>
<bean id="mySessionMonitor"   class="my.app.aspects.SessionMonitoringAspect" />

方面代码

// Aspect monitoring code
@Aspect
public class SessionMonitoringAspect
{
    private Logger fileLogger = Logger.getLogger("myLogger");

    public void initialise()
    {
        fileLogger.info("Initialising SessionMonitoringAspect");
    }

    @Pointcut ("execution (public * *(..))")
    private void anyPublicOperation(){}

    @Pointcut ("within(my.app.session..*)")
    private void inSession(){}

    @Pointcut("anyPublicOperation() && inSession()")
    private void sessionOperation(){}

    @Before("sessionOperation()")
    public void sessionOperationDetected(JoinPoint jp)
    {
        fileLogger.info("Session operation detected - signature: " + jp.getSignature());
    }
}

I am just getting started with Spring AOP in my project and am having some problems with getting Spring AOP working correctly.

I have two objects, TransportImpl and SesssionImpl that I would like to profile via AOP. Both objects(beans) are initialised via Spring. Both beans are implementations of business
interfaces (Transport and Session). I can get Aspects applied to the TransportImpl bean to work well, but those applied to the SessionImpl just do not fire. I can confirm that the
"mySessionMonitor" Aspect is initialised by Spring, and that the SessionImpl object is also initialised without any exceptions or errors.

I have stripped down my PointCuts and Aspect to the most basic form possible. I would have expected the PointCut sessionOperation described below
to fire when the SessionImpl bean is initialised and the init-method initialise is called. But this never happens. What might be going wrong here?

From the configuration file:

<bean id="MyTransport" class="my.app.transport.TransportImpl"   scope="singleton" />                
<bean id="MySession" class="my.app.session.SessionImpl" init-method="initialise" scope="singleton" />

<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="myTransportMonitor" />
    <aop:include name="mySessionMonitor" />
</aop:aspectj-autoproxy>

<bean id="myTransportMonitor" class="my.app.aspects.TransportMonitoringAspect"/>
<bean id="mySessionMonitor"   class="my.app.aspects.SessionMonitoringAspect" />

Aspect code

// Aspect monitoring code
@Aspect
public class SessionMonitoringAspect
{
    private Logger fileLogger = Logger.getLogger("myLogger");

    public void initialise()
    {
        fileLogger.info("Initialising SessionMonitoringAspect");
    }

    @Pointcut ("execution (public * *(..))")
    private void anyPublicOperation(){}

    @Pointcut ("within(my.app.session..*)")
    private void inSession(){}

    @Pointcut("anyPublicOperation() && inSession()")
    private void sessionOperation(){}

    @Before("sessionOperation()")
    public void sessionOperationDetected(JoinPoint jp)
    {
        fileLogger.info("Session operation detected - signature: " + jp.getSignature());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗心人 2024-09-18 23:25:03

我个人更喜欢将 Aspect 切入点配置放在应用程序上下文中,并且您缺少 aop:aspect 中的 ref。

 <bean id="mySessionMonitor"   class="my.app.aspects.SessionMonitoringAspect" />

 <aop:config proxy-target-class="true">
  <aop:aspect ref="mySessionMonitor">
   <aop:pointcut id="around" expression="execution (public * *(..))"/></aop:pointcut>
   <aop:around pointcut-ref="around" method="aroundAdvice"/>
  </aop:aspect>
 </aop:config>

I personally prefer to put the Aspect pointcut configuration in the application context, and you are missing the ref in aop:aspect.

 <bean id="mySessionMonitor"   class="my.app.aspects.SessionMonitoringAspect" />

 <aop:config proxy-target-class="true">
  <aop:aspect ref="mySessionMonitor">
   <aop:pointcut id="around" expression="execution (public * *(..))"/></aop:pointcut>
   <aop:around pointcut-ref="around" method="aroundAdvice"/>
  </aop:aspect>
 </aop:config>
渡你暖光 2024-09-18 23:25:03

如果您更喜欢基于注释的配置,请参阅我编写的这个示例。 XML 配置中所需的只是

If you prefer annotation-based configuration, see this example I wrote. All you'll need in your XML configuration is <aop:aspectj-autoproxy />.

风吹雨成花 2024-09-18 23:25:03

我希望 initialise() 方法不是 my.app.session.SessionImpl 的一部分。如果是这样,您的方面的 initialise() 方法将不会触发。因为你不能从bean类中调用aspect的方法。否则请附上 my.app.session.SessionImpl 代码以供详细了解。

I hope initialise() method is not part of my.app.session.SessionImpl. If so initialise() method of your Aspect will not fire. Because you cannot call aspect's method from bean class. Otherwise please attach my.app.session.SessionImpl code for detail understanding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文