0

I am wondering, why Spring's implementation of the @Component Annotation has RetentionPolicy.RUNTIME. Here's the official implementation from github

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
    String value() default "";
}

In my thought, the scanning for all Spring-Components annotated with @Component would be done at compile-time and all the results (annotated classes) would somehow be stored inside the ApplicationContext. I guess I am wrong, but why does this annotation need to be RetentionPolicy.Runtime?

wanshurst
  • 13
  • 1

1 Answers1

4

The component scanning is done at the startup, not at the compilation. Also, the aspects (AOP) are created at the runtime

Oreste Viron
  • 3,592
  • 3
  • 22
  • 34
  • First of all, thanks. SpringDocs say: `Weaving is the process of linking aspects with other objects to create the advised proxy objects. [...] Spring AOP performs weaving at the runtime.` and: `AOP proxy: an object created by the AOP framework in order to implement the aspect contracts.` So if I have a class annotated with `@Aspect` and `@Component`, why does `@Component` need to be RUNTIME? I would expect `@Aspect` to be runtime but not `@Component`. – wanshurst Sep 06 '22 at 10:56
  • @wanshurst : Spring will search the classes annotated with Component, Service, Repository, Controller... etc... Spring is not searching the Aspect classes. But if a Component is annotated with Aspect, spring will wire the aspects. All at RUNTIME – Oreste Viron Sep 06 '22 at 11:43