Java 为Spring PetClinic添加新特性
在中,尝试将新的方面类添加到org.springframework.samples.petclinic中的方面包中 我的aspect类如下所示:Java 为Spring PetClinic添加新特性,java,spring,aop,aspectj,Java,Spring,Aop,Aspectj,在中,尝试将新的方面类添加到org.springframework.samples.petclinic中的方面包中 我的aspect类如下所示: package org.springframework.samples.petclinic.aspects; import org.apache.openjpa.jdbc.sql.Join; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; imp
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()));
}
}
<?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>
然后,我继续在/resources/META-INF中的aop.xml中使用aspect,如下所示:
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()));
}
}
<?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(方面)添加到webapp/WEB-INF/spring文件夹中的
applicationContext jdbc.xml
来解决这个问题。我不知道为什么会这样?有人能给我解释一下吗-谢谢我不确定Aspectj的编织配置,但在Spring AOP中,您可以使用
<aop:aspectj-autoproxy/>
有关详细信息您确定日志记录配置吗?你可以用sop来排除这种可能性。其他方面是否有效?对于aspectj集成,您需要使用javaagent命令行运行它argument@gkamal-谢谢。是 啊log4j.properties中的日志配置设置为INFO。看起来其他方面都在工作。什么是SOP?