Reputation: 33004
Spring Boot 2.3.12 (I can't update to a newer version for reasons out of my control).
I have defined my main application class with specific scan base packages like this:
@SpringBootApplication(scanBasePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
}
)
@ComponentScan(
excludeFilters = {
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
}
)
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.run(args);
}
}
What I'm trying to accomplish is both:
A) include a package outside the application's base package (hence the org.otherpackage.ComponentScanMarker.class
reference in the @SpringBootApplication
annotation)
and
B) exclude the HateoasConfiguration
class completely*.
I've also tried this:
@SpringBootApplication
@ComponentScan(
basePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
},
excludeFilters = {
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
}
)
That results in HateoasConfiguration
being loaded despite the excludeFilters
.
Another option I tried:
@SpringBootApplication(scanBasePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
},
exclude = HateoasConfiguration.class
)
That results in an exception at startup with the message:
The following classes could not be excluded because they are not auto-configuration classes:
- org.springframework.hateoas.config.HateoasConfiguration
I can't get it to work, no matter what combination of annotation properties I try. Either HateoasConfiguration
gets loaded despite the attempt to exclude it, or @Component
s in org.otherpackage
don't get loaded. I've looked at a few different similar questions and answers, but none of them include the need for both goals.
How can I accomplish both needs, to include multiple base packages for component scanning, and exclude a specific @Configuration
class that's on the classpath?
* This question really has nothing to do with Spring HATEOAS, it's just an example of a @Configuration
class that is on the classpath but I want Spring Boot to ignore. Here are the annotations present on that class (source code here):
@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {
Upvotes: 0
Views: 12427
Reputation: 53
I've been looking for a solution for a similar problem myself. Dropping it here for future developers.
The underlying problem is that @SpringBootApplication
performs a full component scan itself, so your own @ComponentScan
annotation does not have the desired effect.
The solution for me was to eject @SpringBootApplication
and just do what it does internally (with our desired modifications) as it's just a convenience shorthand.
For your case, try:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
basePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
},
excludeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
})
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.run(args);
}
}
Upvotes: 0
Reputation: 3749
Have you tried this ?
@SpringBootApplication(scanBasePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
},
exclude = { HateoasConfiguration.class }
)
@ComponentScan(
excludeFilters = {
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
}
)
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.run(args);
}
}
Upvotes: 0