繁体   English   中英

Spring BeanCreationException:bean 创建过程中出现意外异常

[英]Spring BeanCreationException: Unexpected exception during bean creation

我有一个通知库(用 Spring Boot、gradle、Java 编写),它有一个 Service 类(一个接口)和一个 implementation 类(它扩展了 Service 类并覆盖了方法)。

服务接口:

public interface NotificationService {
    void sendNotification(String title, String message, List<String> ids);

实现类:

@Component
public class NotificationServiceImpl implements NotificationService {
    private String notificationServiceURL;

    public NotificationServiceImpl(@Value("${notificationService.url}") String notificationServiceURL) {
        this.notificationServiceURL = notificationServiceURL;
    }

    public void sendNotification(String title, String message, List<String> employeeIds) {
        // Some functionality
    }
}

应用程序.yaml

notificationService:
  url: "someURL"

我已将此库打包为 JAR 并导入到某个项目中,该项目也是 Spring Boot 项目。

现在,在主类中,我为导入的库类所在的路径添加了组件扫描:

@ComponentScan({"com.abchealth.xyz.red", "com.abchealth.xyz.red.service", "com.abchealth.xyz.red.service.serviceImpl"})
public class SecondProject {
    public static void main(String[] args) {
        SpringApplication.run(SecondProject.class, args);
    }
}

然后在其中一个内部类中,我创建了一个 NotificationService 对象

@Slf4j
@Component
@StepScope
public class SomeInnerClass implements Tasklet {

    @Autowired
    NotificationService notificationService;

    // some code

当我运行应用程序时,它给了我这个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notificationServiceImpl' defined in URL [jar:file:/Users/usera/.gradle/caches/modules-2/files-2.1/com.abchealth.xyz.red/notification-lib/1.0.0-12/f973790603b7c261b2f6a693e83ca3e8f3f021de/notification-lib-1.0.0-12-scan.jar!/com/abchealth/xyz/red/service/serviceImpl/NotificationServiceImpl.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'notificationService.url' in value "${notificationService.url}"
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]

注意:如果我通过创建 NotificationService 对象直接在 Notification 库中测试它,它工作得很好! 问题是它何时被导入到其他项目中。

您需要将notificationService.url属性添加到您的 SecondProject 的 application.yml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM