deD*_*ari 7 java spring jpa spring-data-jpa spring-boot
我已将应用程序的整个实体和存储库接口打包到一个 jar 中。存储库是用@Repository 注释编写的:
@Repository
public interface InternalUserRepository extends JpaRepository<InternalUser, Long>{
}
Run Code Online (Sandbox Code Playgroud)
我已经将这个 jar 文件包含在我的 spring 启动应用程序中,并尝试从控制器自动连接这样的接口:
@RestController
public class AuthenticationController {
@Autowired
AuthenticationService authenticationService;
@Autowired
InternalUserRepository internalUserRepository;
@GetMapping("/")
public String home() {
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud)
我的主应用程序类是这样写的:
@SpringBootApplication
@EnableJpaRepositories
@ComponentScan("com.cdac.dao.cdacdao.*")
public class CdacAuthenticationMgntApplication {
public static void main(String[] args) {
SpringApplication.run(CdacAuthenticationMgntApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
存储库没有自动装配。当我启动 Spring boor 应用程序时,出现以下错误:
***************************
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.
Run Code Online (Sandbox Code Playgroud)
有没有人尝试过类似的架构?
dun*_*nni 10
如果您的 JPA 存储库与 Spring Boot 应用程序类位于不同的包中,则必须在EnableJpaRepositories注释上指定该包,而不是Component:
@EnableJpaRepositories("com.cdac.dao.cdacdao")
Run Code Online (Sandbox Code Playgroud)
您指定的包ComponentScan用于将类检测为常规 Spring bean,而不是存储库接口。
Mạn*_*yễn -1
我记得,@ComponentScan应该采用包的完整路径,所以我认为你的package.*不起作用。
尝试改用类型安全的组件扫描:
// 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 {
Run Code Online (Sandbox Code Playgroud)
或者你可以尝试@EnableJpaRepositories("com.cdac.dao.cdacdao")
不管怎样,你应该选择最外层包中的类(Spring也会尝试在这些组件扫描包的子包中查找bean)
| 归档时间: |
|
| 查看次数: |
4163 次 |
| 最近记录: |