How to configure component scanning to get beans from specifics packages and single classes from another? I'm trying that way, but TestClass1.class and TestClass2.class could not be found:
@ComponentScan(
basePackages = {"com.example.package1", "com.example.package2"},
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {com.example.package3.TestClass1.class, com.example.package3.TestClass2.class}))
The attribute basePackageClasses
does not work as you think. It does not scan only the configured classes, it scans the complete package of each class.
Which means that:
@ComponentScan( basePackages = {"com.example.package1", "com.example.package2"},
basePackageClasses = {com.example.package3.TestClass1.class, com.example.package3.TestClass2.class})
is exactly the same as:
@ComponentScan( basePackages = {"com.example.package1", "com.example.package2", "com.example.package2"} )
I would say that the simplest and cleanest way to do this is to use @Bean to define each bean:
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.package1", "com.example.package2"})
public class Application {
@Bean
public TestClass1 testClass1() {
// object creation
}
@Bean
public TestClass2 testClass2() {
// object creation
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With