Spring Boot: autowire beans from library project

maret picture maret · Jan 23, 2017 · Viewed 16.2k times · Source

I'm struggling to autowire beans from my custom library, imported with gradle. after reading couple of similar topics I am still unable to find solution.

I have a Spring Boot project that depends on another project (my custom library with Components, Repositories etc...). This library is a Spring non-runnable jar, that consists primarily of domain Entities and Repositories. It doesn't have runnable Application.class and any properties...

When I start the application I can see that My 'CustomUserService' bean (from the library) is trying to be initialized, but the bean autowired in it failed to load (interface UserRepository)...

Error:

Parameter 0 of constructor in com.myProject.customLibrary.configuration.CustomUserDetailsService required a bean of type 'com.myProject.customLibrary.configuration.UserRepository' that could not be found.

I've even tried to set 'Order', to load it explicitly (with scanBasePackageClasses), scan with packages and marker classes, add additional EnableJPARepository annotation but nothing works...

Code example (packages names were changed for simplicity)

package runnableProject.application;

import runnableProject.application.configuration.ServerConfigurationReference.class
import com.myProject.customLibrary.SharedReference.class

//@SpringBootApplication(scanBasePackages = {"com.myProject.customLibrary", "runnableProject.configuration"}) 
//@EnableJpaRepositories("com.myProject.customLibrary")  

@SpringBootApplication(scanBasePackageClasses = {SharedReference.class, ServerConfigurationReference.class})   
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

Classes from the library:

package com.myProject.customLibrary.configuration;

import com.myProject.customLibrary.configuration.UserRepository.class;

@Service
public class CustomUserDetailsService implements UserDetailsService {
    private UserRepository userRepository;    

    @Autowired
    public CustomUserDetailsService(UserRepository userRepository) {
        this.userRepository = userRepository;       
    }
...

package myProject.customLibrary.configuration;

@Repository
public interface UserRepository extends CustomRepository<User> {
    User findByLoginAndStatus(String var1, Status var2);

    ...
}

Answer

maret picture maret · Jan 23, 2017

Just found the solution. Instead of defining base packages to scan from separate library, I've just created configuration class inside this library with whole bunch of annotation and imported it to my main MyApplication.class:

package runnableProject.application;    

import com.myProject.customLibrary.configuration.SharedConfigurationReference.class

@SpringBootApplication
@Import(SharedConfigurationReference.class)
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}
package com.myProject.customLibrary.configuration;

@Configuration
@ComponentScan("com.myProject.customLibrary.configuration")
@EnableJpaRepositories("com.myProject.customLibrary.configuration.repository")
@EntityScan("com.myProject.customLibrary.configuration.domain")
public class SharedConfigurationReference {}