【问题标题】:How to connect another project A to project B as a depedency in java springboot?java - 如何在java spring boot中将另一个项目A连接到项目B作为依赖项?
【发布时间】:2018-09-11 16:29:08
【问题描述】:

我有依赖于项目 A (com.webfx) 的项目 B (com.embos)。我将项目 A 作为项目 B 中的依赖项导入。 这是我的项目 B 的 pom:

    <dependency>
        <groupId>com.webfx</groupId>
        <artifactId>com.webfx</artifactId>
        <version>1.0</version>
        <type>jar</type>
    </dependency>

当我清理和构建时,它是返回错误:

java.lang.IllegalStateException: 无法加载 ApplicationContext 原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“pilahServiceImpl”的bean时出错:通过字段“auditTrailService”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.webfx.sys.bpm.AuditTrailService”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“com.webfx.sys.bpm.AuditTrailService”类型的合格bean:预计至少有1个符合自动装配候选资格的bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

结果:
错误:
EmbosApplicationTests.contextLoads » IllegalState 无法加载 ApplicationCo...

而在我的PilahServiceImpl 中,那些使用auditTrailService 的是@Autowired.

这是我的 PilahServiceImpl 类:

@Service
public class PilahServiceImpl implements PilahService {

    @Autowired
    private PilahRepository repoPilah; 
    @Autowired
    private EmbosRepository repoEmbos;
    @Autowired
    private AuditTrailService auditTrailService;
...
}

为什么会这样?感谢关注

【问题讨论】:

    标签: java maven spring-boot dependencies


    【解决方案1】:

    您忘记将 AuditTrailService 的依赖项注入到 ApplicationContext。您必须使用(@Bean、@Component 等)和 @ComponentScan 之一添加 AuditTrailService 实例,以扫描定义注入的类/包。 通过使用以下代码,我能够复制此异常:

    @RestController
    @EnableAutoConfiguration
    @ComponentScan("com.webfx.sys.bpm")
    public class Example {
        @Autowired
        TestDao dao;
    }
    

    在 TestDao 上添加 @Component 注解并在 Example 上添加 @ComponentScan 修复它。

    【讨论】:

    • 谢谢你,先生,我会试试的
    【解决方案2】:

    如果您在服务器启动时使用配置加载此 bean。只需在你的项目中添加这个类,它就会开始加载 bean。

    添加配置

    @Configuration
    public class MyConfiguration {
    @Bean
    public AuditTrailService auditTrailService() {
    
        return new AuditTrailService();
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2019-07-28
      • 2018-08-04
      • 1970-01-01
      • 2017-02-26
      相关资源
      最近更新 更多