Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude bean from loading?

I have a problem integrating external library with Spring. It contains a class annotated with @Configuration and it has a method annotated with @Bean. I don't want it to be instantiated (it's not needed and introduces dependency on a Spring Boot, which I don't use.

Unfortunately this @Configuration-annotated class is used elsewhere in the library (required by the class type, not interface type, so I need to instantiate exactly this class).

I exluded it's package from auto-scanning, I'm not importing it directly. Just constructing it by hand and registering in own configuration as a bean.

So, to make story short - I need to register a bean, but exclude it from annotation scnanning (to not process it's @Bean-annotated methods). Any way for doing this?

like image 890
Rafał Wrzeszcz Avatar asked Jul 09 '16 19:07

Rafał Wrzeszcz


People also ask

How to exclude specific classes from being scanned into a bean?

You can exclude specific classes from being scanned into beans with the excludeFilters parameter of the @ComponentScan annotation. This allows for the exclusion of specific classes, classes matching a given pattern... For example, to exclude firstClass and secondClass you would write:

How do I exclude a bean from autowiring in spring?

Excluding Bean From Autowiring in Spring You can exclude a bean from autowiring in Spring framework per-bean basis. If you are using Spring XML configuration then you can exclude a bean from autowiring by setting the autowire-candidate attribute of the <bean/> element to false.

When to load beans or modules into the application context?

When building a Spring Boot app, we sometimes want to only load beans or modules into the application context if some condition is met. Be it to disable some beans during tests or to react to a certain property in the runtime environment.

Should this Bean ever load?

This bean should never load, unless someone has created a Windows / Unix hybrid that I’m not aware of. Note that the @Conditional annotation cannot be used more than once on a single method or class. So, if we want to combine multiple annotations this way, we have to use custom @ConditionalOn... annotations, which do not have this restriction.


1 Answers

If you mean you have library in your maven, gradle, then you can exclude spring beans from being initialized. I found exclude @Component from @ComponentScan

@Configuration
@EnableSpringConfigured
@ComponentScan(
    basePackages = {"com.example"},
    excludeFilters = {
        @ComponentScan.Filter(
            type = FilterType.ASSIGNABLE_TYPE,
            value = Foo.class)})
public class MySpringConfiguration {

}

And if you do not want to include transitive dependencies, thus you do not want your dependency to add other dependencies it uses, you have to exclude them from your dependncy (in maven, gradle).

like image 183
Yan Khonski Avatar answered Oct 06 '22 11:10

Yan Khonski