12

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?

Rafał Wrzeszcz
  • 1,996
  • 4
  • 23
  • 45
  • You can refer the below link .Hope it helps. http://stackoverflow.com/questions/18992880/exclude-component-from-componentscanhttp://stackoverflow.com/questions/18992880/exclude-component-from-componentscan – JSav Jul 09 '16 at 21:13
  • 1
    @JSav Please read the problem description. I'm not auto-scanning the package, I'm creating a bean by-hand and just because it's injected into container, it's being processed. – Rafał Wrzeszcz Jul 09 '16 at 21:33
  • @RafałWrzeszcz, what is it mean "registering a bean without annotation scanning" ? Then how can u register it ? You need a method with '@Bean' annotation that crate and instance of the class, or annotate the class with '@Componen', '@Service' etc. annotation for annotation scanning to find it ? – Gokhan Oner Jul 10 '16 at 00:12
  • 1
    Does `@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = TheBean.class) )` will help you? – Rae Burawes Jul 10 '16 at 07:08
  • @GokhanOner I *DO NOT* want it to be registered. – Rafał Wrzeszcz Oct 19 '16 at 15:35
  • @RaeBurawes Please read - it's not imported through scanning. – Rafał Wrzeszcz Oct 19 '16 at 15:35

1 Answers1

7

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).

Arnaud Claudel
  • 3,000
  • 19
  • 25
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114