Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create auto configure spring library to spring-boot application

I am creating a library which uses spring 4.3.0.One of the spring-boot application will consume this library. Currently i am using @ComponentScan in the main class of spring-boot application to scan the beans inside library instead i want to auto-configure it.So what i did is i created a configuration class inside the library and declared @ComponentScan in the configuration file.

After consuming the library in spring-boot application it is not scanning the beans inside library and throws,

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sample.book.client.service.BookService] found for dependency [com.sample.book.client.service.BookService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
... 33 common frames omitted   

How to resolve this error ? Why Spring scanning @service classes before @Configuration?

Your help should be appreciable and i will provide code-samples if needed.

like image 935
VelNaga Avatar asked Jan 19 '26 17:01

VelNaga


1 Answers

It seems to me that most probable cause is that your library resides in a different package than your spring boot application (and its sub-packages). When annotating a class with @SpringBootApplication you also get @ComponentScan annotation set to its default (that is scanning components in a package where a given class resides).

Personally, I prefer to create a @Configuration annotated class in my library projects. Such class is responsible for proper library setup (declaring component scan and so on). Later, in dependent project I use an @Import annotation to import that configuration class (and corresponding beans).

like image 105
dchrzascik Avatar answered Jan 22 '26 13:01

dchrzascik