java - 尽管组件位于同一包和扫描组件中,但 Spring 未检测到该组件

标签 java spring spring-boot aop

所以我有一个 Spring 应用程序,我使用注释和 @Loggable 自定义注释添加了一个loggingHandler类。我已经成功记录了 @RestController 类中定义的方法调用,但是,注释为 @Component 的类似乎未被 spring 检测到...

这是部分代码:

package company;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;


@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan({"company", "document", "logger", "model.bodyComponents"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

然后是API

package company;

@RestController
public class Api {

    @PostMapping(value = "/", consumes = MediaType.APPLICATION_XML_VALUE)
    @Loggable
    public ResponseEntity<?> convertXmlToPdf(HttpServletRequest request) {
                // some code
                root.render(outputStream); //it is called correctly
                // some code
    }

然后调用方法 render :

package company;

@Component
public class Root {

    @Loggable
    public void render(OutputStream target) throws IOException, ParseException, TypesetElementWidthException, ClassNotFoundException {

     //some code  

    }

}

然后是 LoggingHandler:

@Aspect
@Configuration
public class LoggingHandler {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut("@annotation(logger.Loggable)")
    public void pcLoggable(){
    }

    @Before("@annotation(logger.Loggable)")
    public void beforeAnnotLog(JoinPoint joinPoint){

        log.info(joinPoint.getSignature() + "Something will be called called with loggable annot.", joinPoint);
        System.out.println("Test Loggable annotation Before the call");

    }

    @After("@annotation(logger.Loggable)")
    public void annotLog(JoinPoint joinPoint){

        log.info(joinPoint.getSignature() + "Something is called with loggable annot.", joinPoint);
        System.out.println("Test Loggable annotation");

    }

}

最后是可记录注释:

package logger;

import java.lang.annotation.*;

@Target({ElementType.TYPE ,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Loggable {

}

当调用convertXmlToPdf 时,会调用记录器(我发布了一个XML,一切运行正常)。

该方法调用 Root.render 方法,但没有记录任何内容,尽管 Root 是一个组件并且渲染被 @Loggable 注释。所以这让我认为 spring 没有检测到 Root 类作为组件......

最佳答案

How does the Api obtain a reference to a Root class? My guess you are doing a new Root() in there somewhere instead of getting the Spring managed instance. – M. Deinum Jul 11 at 10:49

是的,有一个新的 Root(),但我不确定为什么我应该使用 spring 托管实例... – Cromm 7 月 11 日 11:04

答案很简单:因为 Spring AOP 只适用于 Spring 组件。它创建动态代理,以便向它们注册方面或顾问,并在调用代理方法时执行它们。通过实例化new Root()您正在规避代理创建,因此您不能指望任何 Spring AOP 方面可以在那里工作。为了将 AOP 应用于非 Spring 管理的对象,您需要使用完整的 AspectJ。

@M。 Deinum,我写这个答案是为了结束这个话题。我知道你本来可以自己写的,但在两周多的时间里没有这样做,所以请原谅我从你那里偷了这个。请随意写下您自己的答案,然后我将删除我的答案,您的答案就可以被接受。

关于java - 尽管组件位于同一包和扫描组件中,但 Spring 未检测到该组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56987029/

相关文章:

java - 对象关系映射的缺点

java - android java A* 需要寻路帮助

java - Phidg​​et RFID 未连接(ANDROID 和 PHIDGET)

使用 Nulls_Last 处理进行 Spring 排序不起作用

java - Spring Boot中读取url编码数据

spring-boot - Spring 单元测试休息 Controller

java - 用于 Java 的 MKL 加速数学库

java - 使用 Spring 解码主体参数

java - 当我的 Hibernate 事务由 Spring 管理时如何启用 Hibernate 拦截器?

java - @RequestMapping 和 @PostMapping 有什么区别