Can't autowire repository from an external Jar into Spring Boot App

大憨熊 提交于 2021-02-07 06:52:17

问题


I have packaged my entire entities of the application and the repository interfaces into one jar. The repositories are written with @Repository annotation:

@Repository
public interface InternalUserRepository extends JpaRepository<InternalUser, Long>{

}

I have included this jar file inside my spring boot application and trying to autowire the interface like this from a controller:

@RestController
public class AuthenticationController {

    @Autowired
    AuthenticationService authenticationService;

    @Autowired
    InternalUserRepository internalUserRepository;


    @GetMapping("/")
    public String home() {
        return "Hello World!";
    }

}

My Main app class is written like:

@SpringBootApplication
@EnableJpaRepositories
@ComponentScan("com.cdac.dao.cdacdao.*")
public class CdacAuthenticationMgntApplication {

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

The repository is not getting autowired. When I am firing up the Spring boor application I am getting the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field internalUserRepository in 
com.cdac.user.cdacauthenticationmgnt.controller.AuthenticationController required a bean of type 'com.cdac.dao.cdacdao.repository.InternalUserRepository' that could not be found.


Action:

Consider defining a bean of type 'com.cdac.dao.cdacdao.repository.InternalUserRepository' in your configuration.

Have anyone tried similar architecture like this?


回答1:


If your JPA repositories are in a different package than your Spring Boot application class, you have to specify that package on the EnableJpaRepositories annotation, not Component:

@EnableJpaRepositories("com.cdac.dao.cdacdao")

The package you specified on ComponentScan is for detecting classes as regular Spring beans, not repository interfaces.




回答2:


As I remember, @ComponentScan should take a complete path to a package, so I think that your package.* does not work.

Try to use type-safe component scan instead:

// You refer to your packages of your base project and your module here. 
// Choose the class so that their package is cover all child package
@SpringBootApplication(scanBasePackageClasses = {xxx. InternalUserRepository.class, xxx.CdacAuthenticationMgntApplication.class}) 

@EnableJpaRepositories
// No need to explicit @ComponentScan
public class CdacAuthenticationMgntApplication {

Or you can try @EnableJpaRepositories("com.cdac.dao.cdacdao")

Either way, you should pick the class in the most outer package (Spring will aslo try to find bean in sub package of those component scan packages)




回答3:


Annotation @SpringBootApplication has supported all function So we don't need config manually

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })


来源:https://stackoverflow.com/questions/50230962/cant-autowire-repository-from-an-external-jar-into-spring-boot-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!