无法将存储库从外部Jar自动连接到Spring Boot App [英] Can't autowire repository from an external Jar into Spring Boot App
问题描述
我已经将应用程序的整个实体和存储库接口打包到一个jar中.存储库使用@Repository批注编写:
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>{
}
我已经将这个jar文件包含在我的spring boot应用程序中,并试图从控制器中像这样自动连接接口:
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);
}
}
存储库未自动连接.当我启动Spring boor应用程序时,出现以下错误:
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?
推荐答案
如果您的JPA信息库与Spring Boot应用程序类不在同一个包中,则必须在EnableJpaRepositories
批注(而不是Component
)上指定该包:
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")
您在ComponentScan
上指定的包用于将类检测为常规Spring Bean,而不是存储库接口.
The package you specified on ComponentScan
is for detecting classes as regular Spring beans, not repository interfaces.
这篇关于无法将存储库从外部Jar自动连接到Spring Boot App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!