Exclude @Configuration from Spring Boot Application

We Are Going To Discuss About Exclude @Configuration from Spring Boot Application. So lets Start this Java Article.

Exclude @Configuration from Spring Boot Application

  1. Exclude @Configuration from Spring Boot Application

    You can try this it works for me:
    @SpringBootApplication @ComponentScan(excludeFilters = {@ComponentScan.Filter( type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})

  2. Exclude @Configuration from Spring Boot Application

    You can try this it works for me:
    @SpringBootApplication @ComponentScan(excludeFilters = {@ComponentScan.Filter( type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})

Solution 1

You can try this it works for me:

@SpringBootApplication
@ComponentScan(excludeFilters  = {@ComponentScan.Filter(
              type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})

Original Author preetham Of This Content

Solution 2

Auto-config packages lives under org.springframework.boot.autoconfigure. That’s the reason why you can’t do:

@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})

Spring does what you tell to do. You are calling:

@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})

Therefore Spring won’t load any of those Worker classes. That’s why Spring doesn’t “execute” your classes annotated with @Configuration.

That said, what you are trying to do doesn’t make sense to me. Sounds like you have “modules” (java classes), but all of them are part of the same spring context. If you have a single Spring Context, then you can tell Spring to load some @Configuration classes and not some others. Then, from your “modules”, you can inject whatever you need. Module 1 will inject Beans from Module 3, but Module 2 won’t. Simple as that.

If for some reason you really need to prevent Module 2 accessing to beans from Module 3, but still keep Module 3 visible from Module 1, then I would separate Module 1 and Module 2 in two different Spring Boot Apps, and Module 3 becomes common code. But this approach may break your current architecture.

UPDATE ON Fri Mar 29 2019

Try this:

@SpringBootApplication
@ComponentScan(basePackages = { "com.myapp" }, excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { MyClass2.class }) })

It worked for me. I have MyClass and MyClass2, and MyClass is loaded and MyClass2 is not. I tried it with Spring Boot 1.5.9.RELEASE and Spring Bom for all the dependencies.

Original Author Perimosh Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment