无法使用Spring Data JPA正确连接@Service,找不到依赖项

时间:2016-09-20 23:09:09

标签: java spring hibernate spring-data spring-data-jpa

我在尝试设置库中的@ServiceArchiveService时遇到问题,我可以调用它App1,我的第二个应用程序App2使用它。 / p>

我已成功设置@Repository中的App1 @Autowired App2 ArchiveService @Service @Autowired {/ 1}}。

现在我正在尝试设置@Repositories ArchiveService,其中包含多个HomeController存档App2

当我尝试在我Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'archiveService': No qualifying bean of type [com.miw.mcb.server.service.ArchiveService] found for dependency [com.miw.mcb.server.service.ArchiveService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. 的{​​{1}}中引用No qualifying bean of type [app1.server.service.ArchiveService] found for dependency [app1.server.service.ArchiveService] 表单时,问题就出现了

@Service

这似乎很奇怪,它说

ArchiveService

问题

我为HomeController设置@RepositoryHomeController有什么问题, @ComponentScan(basePackages = "com.miw.mcb.server.service.archived") 找不到@Service用于@Repository @Transactional public interface TestRunRepository extends PagingAndSortingRepository<TestRun, Long> { } 的{​​{1}} 1}}。

更新

我将服务包的组件扫描添加到App2。

@Repository
@Transactional
public interface ArchivedTestRunRepository extends CrudRepository<ArchivedTestRun, Long> {

}
....  (all of the archive repositories are the same)

我将不得不做一些阅读,看看这是否正在做我打算做的事情。还有另一种方法可以加载@Service public class ArchiveService { // setup Archive repositories @Autowired private ArchivedTestRunRepository archivedTestRunRepository; @Autowired private ArchivedTestSuiteRepository archivedTestSuiteRepository; 吗?我知道使用@EnableJpaRepositories适用于存储库。

谢谢

以下是类和弹簧配置的细分。

APPLICATION 1类,它们是App2依赖库的一部分

TestRunRepository

@RestController
public class HomeController {


    @Autowired
    private TestRunRepository testRunRepository;

    @Autowired
    private ArchiveService archiveService;
    .......

}

ArchivedTestRunRepository

@Configuration
@EntityScan({"xxx.app1.model", "xxx.app1.model.archived"})
@EnableJpaRepositories({ "xxx.app1.dao.repository", "xxx.app1.dao.repository.archived", "xxx.app1.service" })
@SpringBootApplication
public class App2 {
    ...
}

ArchiveService

@EnableJpaRepositories

APPLICATION 2

的HomeController

package

App2的AppConfig

subpackage

另一方面,我不确定我是否真的需要为org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'archiveService': No qualifying bean of type [app1.server.service.ArchiveService] found for dependency [app1.server.service.ArchiveService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [app1.server.service.ArchiveService] found for dependency [app1.server.service.ArchiveService]: 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) at app2.adbservice.Main.main(Main.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [app1.server.service.ArchiveService] found for dependency [app1.server.service.ArchiveService]: 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) ... 27 common frames omitted 列出“xxx.app1.dao.repository”,“xxx.app1.dao.repository.archived”或者是否需要父value它将引入value

堆栈跟踪

accounts

1 个答案:

答案 0 :(得分:0)

是否可以尝试使用@SpringBootApplication替换App2中的@SpringBootApplication(scanBasePackages = {"app1.server.service"})注释。我认为应用程序仅扫描实体和存储库bean,因为当前配置。

使用@SpringBootApplication(scanBasePackages = {"app1"})可能会更有用,因为它不仅会扫描服务包,还会扫描整个app1包。