向 Spring PetClinic 添加新方面
尝试将新的方面类添加到 org.springframework.samples.petclinic 中的方面包中。
我的方面类如下:
package org.springframework.samples.petclinic.aspects;
import org.apache.openjpa.jdbc.sql.Join;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.samples.petclinic.context.SessionContext;
import java.util.Date;
@Aspect
public class MethodLogAspect {
Logger logger = LoggerFactory.getLogger(MethodLogAspect.class);
@Pointcut("execution(* org.springframework.samples..*.*(..))")
public void methodLogging(){}
@Before("methodLogging()")
public void logMethodStart(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String className = joinPoint.getTarget().getClass().getName();
logger.info("class name: "+className+"invoked method:"+methodName+" at "+ ((new Date()).getTime()));
}
@After("methodLogging()")
public void logMethodEnd(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String className = joinPoint.getTarget().getClass().getName();
logger.info("class name: "+className+"finished invoking method:"+methodName+" at "+ ((new Date()).getTime()));
}
}
然后,我继续在 /resources/META-INF 中的 aop.xml 中进行方面分析,如下所示:
<?xml version="1.0"?>
<!-- Custom aspects for the PetClinic sample application -->
<aspectj>
<weaver>
<include within="org.springframework.samples.petclinic..*"/>
</weaver>
<aspects>
<aspect name="org.springframework.samples.petclinic.aspects.UsageLogAspect"/>
<aspect name="org.org.springframework.samples.petclinic.aspects.MethodLogAspect"></aspect>
<concrete-aspect name="org.springframework.samples.petclinic.aspects.ApplicationTraceAspect"
extends="org.springframework.samples.petclinic.aspects.AbstractTraceAspect">
<pointcut name="traced" expression="execution(* org.springframework.samples..*.*(..))"/>
</concrete-aspect>
</aspects>
</aspectj>
当我构建 war 并部署它时,我的方面中指定的输出都不会显示在日志。我不确定我在这里错过了哪一步。我还觉得我不明白一切如何联系在一起的机制。有人可以指出我缺少什么并给我一个正确的方向推动。
谢谢
编辑:
我可以通过将 bean(aspect) 添加到 webapp/WEB-INF/spring 文件夹中的 applicationContext-jdbc.xml 来解决此问题。我不确定为什么这会起作用?有人可以给我一个解释吗? -谢谢
In tried to add a new aspect class to the aspects package in org.springframework.samples.petclinic.
My aspect class is as follows:
package org.springframework.samples.petclinic.aspects;
import org.apache.openjpa.jdbc.sql.Join;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.samples.petclinic.context.SessionContext;
import java.util.Date;
@Aspect
public class MethodLogAspect {
Logger logger = LoggerFactory.getLogger(MethodLogAspect.class);
@Pointcut("execution(* org.springframework.samples..*.*(..))")
public void methodLogging(){}
@Before("methodLogging()")
public void logMethodStart(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String className = joinPoint.getTarget().getClass().getName();
logger.info("class name: "+className+"invoked method:"+methodName+" at "+ ((new Date()).getTime()));
}
@After("methodLogging()")
public void logMethodEnd(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String className = joinPoint.getTarget().getClass().getName();
logger.info("class name: "+className+"finished invoking method:"+methodName+" at "+ ((new Date()).getTime()));
}
}
I then went ahead and aspect in the the aop.xml in /resources/META-INF as follows:
<?xml version="1.0"?>
<!-- Custom aspects for the PetClinic sample application -->
<aspectj>
<weaver>
<include within="org.springframework.samples.petclinic..*"/>
</weaver>
<aspects>
<aspect name="org.springframework.samples.petclinic.aspects.UsageLogAspect"/>
<aspect name="org.org.springframework.samples.petclinic.aspects.MethodLogAspect"></aspect>
<concrete-aspect name="org.springframework.samples.petclinic.aspects.ApplicationTraceAspect"
extends="org.springframework.samples.petclinic.aspects.AbstractTraceAspect">
<pointcut name="traced" expression="execution(* org.springframework.samples..*.*(..))"/>
</concrete-aspect>
</aspects>
</aspectj>
When I build the war and deploy it, none of the output specified in my aspect shows up in the log. I am not sure exactly which step I am missing here. I also feel like I do not understand the mechanics of how everything is tied together. Can somebody point out what I am missing and give me a nudge in the right direction.
Thanks
Edit:
I was able to fix this issue by adding the bean(aspect) to the applicationContext-jdbc.xml
in webapp/WEB-INF/spring folder. I am not sure why this would work? Can somebody provide me with an explanation? -Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定 Aspectj 编织配置,但在 Spring AOP 中,您可以使用
@Aspect 注释启用自动检测。阅读 http:// static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj 了解更多信息
I am not sure of Aspectj weaving configurations but in Spring AOP you could use
to enable autodetection of @Aspect annotations . Read http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj for more info