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
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 the application starts, both Component1 and Component2 identified as candidate components. However, only Component1 is created.
Component2 will only execute after I make one of the following changes
- Move it to
com.package , i.e. to Component1 - I declare it as a
@Autowired field in com.package.Configuration
Why does Spring Boot detect the component but not create it in this case? Are there any differences in how @ComponentScan works with respect to detecting and instantiating @Component ?
source share