Spring AOP - 切入点未触发
我刚刚开始在我的项目中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人更喜欢将 Aspect 切入点配置放在应用程序上下文中,并且您缺少 aop:aspect 中的 ref。
I personally prefer to put the Aspect pointcut configuration in the application context, and you are missing the ref in aop:aspect.
如果您更喜欢基于注释的配置,请参阅我编写的这个示例。 XML 配置中所需的只是
。If you prefer annotation-based configuration, see this example I wrote. All you'll need in your XML configuration is
<aop:aspectj-autoproxy />
.我希望
initialise()
方法不是my.app.session.SessionImpl
的一部分。如果是这样,您的方面的initialise()
方法将不会触发。因为你不能从bean类中调用aspect的方法。否则请附上my.app.session.SessionImpl
代码以供详细了解。I hope
initialise()
method is not part ofmy.app.session.SessionImpl
. If soinitialise()
method of your Aspect will not fire. Because you cannot call aspect's method from bean class. Otherwise please attachmy.app.session.SessionImpl
code for detail understanding.