I have a Spring Boot application with the following structure
com.package
Application - annotated with @SpringBootApplication
Configuration - annotated with @Configuration
Component1 - annotated with @Component, constructor annotated with @Autowired
com.package.subpackage
Component2 - annotated with @Component, constructor annotated with @Autowired
My application class is
package com.package;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
When I start the application both Component1 and Component2 are identified as candidate components. However, only Component1 is instantiated.
Component2 will only instantiate when I make either of the following changes
- I move it to
com.packagei.e. the same asComponent1 - I declare it as a
@Autowiredfield incom.package.Configuration
Why does Spring Boot discover the component but not instantiate it in this case? Are there differences in how @ComponentScan works with regards to discovering vs instantiating @Component?