Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot auto configuration with dependency and without @ComponentScan

Spring boot provides @ComponentScan to find packages to be scanned.

I am building a library which has @RestControllers inside with package com.mylib.controller. There are other classes as well with stereotype annotations in different packages.

So, if some one is developing SpringBoot Application with com.myapp base package. He uses my library in his application. He need to mention @ComponentScan("com.mylib") to discover stereotype components of library.

Is there any way to scan components without including library package in @ComponentScan?

As spring-boot-starter-actuator expose its endpoints just with dependency, without defining @ComponentScan. OR any default package which is scanned regardless of application base package.

like image 874
Nitul Avatar asked Feb 15 '18 13:02

Nitul


People also ask

Can we use both @SpringBootApplication and @EnableAutoConfiguration?

A single @SpringBootApplication annotation can be used to enable those three features, that is: @EnableAutoConfiguration : enable Spring Boot's auto-configuration mechanism.

What is the difference between @component and @ComponentScan in Spring?

@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.

What is the difference between @configuration and @AutoConfiguration?

AutoConfiguration classes are run last (meaning after all regular non-autoconfiguration classes) while the order in which Configuration classes are run is indeterminate (except if we use ordering annotations like @Ordered ) To declare a class as an AutoConfiguration they need to be specified as such in the spring.

What is the use of @ComponentScan in Spring boot?

One of the most important annotations in spring is @ComponentScan which is used along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.


Video Answer


1 Answers

You could create a Spring Boot Starter in the same style as the Spring Provided Starters. They are essentially a jar'd library with a a spring.factories file pointing to the @Configuration class to load with some other annotations on there to provide overriding/bean back off (@ConditionalOnMissingBean) and generally provide their own @ConfigurationProperties.

Stéphane Nicoll provided an excellent demo of how to build one.

https://github.com/snicoll-demos/hello-service-auto-configuration

It is also documented in the Spring Boot documentation. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html

The library approach would also work but I see no benefit in not making it a starter. Additionally for any library/starter I'd recommend dropping the @ComponentScan and just defining the beans in a @Configuration. This will work for sterotypes like @RestController etc. will function as normal if you create an @Bean out of it in a configuration.

like image 159
Darren Forsythe Avatar answered Sep 19 '22 16:09

Darren Forsythe