Spring BeanCreationException:在创建bean期间出现意外异常



我有一个通知库(用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
}
}

Application.yaml

notificationService:
url: "someURL"

我已经将这个库打包为JAR,并在一些项目中导入,该项目也是Spring引导项目。

现在,在主类中,我为导入的库类所在的路径添加了Component Scan:

@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的应用程序中。yml

最新更新