Spring 未检测到组件,尽管它们在同一个包和一个 scancomponent 中

Component is not detected by Spring despite being in same package and a scancomponent

所以我有一个 spring 应用程序,我添加了一个 loggingHandler class 使用注释和 @Loggable 自定义注释。我已成功记录在@RestController class 中定义的方法调用,但是,classes 注释为@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
    }

然后调用的渲染方法:

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");

    }

}

最后是 Loggable 注释:

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 没有将根 class 检测为一个组件...

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,我写这个答案是为了结束这个话题。我知道你可以自己写,但在 2 个多星期内没有这样做,所以请原谅我偷了你的东西。随意写下你自己的答案,然后我会删除我的,你可以接受你的。