Spring boot auto configuration with dependency and without @ComponentScan
M

2

8

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.

Milson answered 15/2, 2018 at 13:45 Comment(0)
A
11

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.

Absolutism answered 15/2, 2018 at 20:3 Comment(2)
Agree with this answer including the mention to dropping @ComponentScan in favor of @Configuration files with @ConditionalOnXXXX annotations. I have also created a blog post about creating a custom Spring Boot starter at tech.asimio.net/2017/06/29/…Supervisory
Thank you! For me adding @ComponentScan caused a lot of weird things to happen in the tests, but adding the controller as a bean in the @Configuration class did the trick.Lyons
G
2

Spring boot starter are special artifacts designed by Spring and used by Spring.
You can check that in the source code that contains mainly a
spring.provides file :

provides: spring-boot-actuator,micrometer-core

I don't know the exact way to process in the same way as Spring Boot Starter but as probably acceptable workaround, you could define in your jar a @Configuration class that specifies @ComponentScan("com.mylib").

 @Configuration
 @ComponentScan("com.mylib")
 public class MyLibConfig {
     //...
 }

In this way, clients of the jar need "only" to import the @Configuration class :

@Import(MyLibConfig.class)
@Configuration
public class ClientConfig{
  //...
}
Gladiolus answered 15/2, 2018 at 13:55 Comment(4)
True, It will work, But I need to tell my library users to Import my configuration class.Milson
I use @EnableMyLib annotations where I have @Import(MyLibConfig)Forrester
@Forrester where are you getting that annotation from? Only google result is your comment.Hooves
It is a custom annotation.Forrester

© 2022 - 2024 — McMap. All rights reserved.