gpt4 book ai didi

java - 向 Spring PetClinic 添加新方面

转载 作者:行者123 更新时间:2023-12-02 08:04:04 24 4
gpt4 key购买 nike

尝试将新的方面类添加到 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 来解决此问题。我不确定为什么这会起作用?有人可以给我一个解释吗? -谢谢

最佳答案

我不确定 Aspectj 编织配置,但在 Spring AOP 中你可以使用

<aop:aspectj-autoproxy/> 

启用 @Aspect 注释的自动检测。阅读 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj了解更多信息

关于java - 向 Spring PetClinic 添加新方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8449803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com