英文:
Spring BeanCreationException: Unexpected exception during bean creation
问题
我有一个通知库(使用Spring Boot、gradle和Java编写),其中包含一个Service类(一个接口)和一个Implementation类(它扩展了Service类并重写了方法)。
Service接口:
public interface NotificationService {
void sendNotification(String title, String message, List<String> ids);
}
Implementation类:
@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 Boot项目的项目中。
现在,在主类中,我为导入的库类所在的路径添加了Component扫描:
@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对象,它能正常工作!问题出现在将其导入其他项目中。
英文:
I have a notification library (written in Spring Boot, gradle, Java) which has a Service class (an interface) and an Implementation class (which extends the Service class and overrides the methods).
Service interface:
public interface NotificationService {
void sendNotification(String title, String message, List<String> ids);
Implementation class:
@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"
I have packaged this library as a JAR and imported in some project which is also Spring boot project.
Now, in the main class, I added Component Scan for the path where the imported library classes are:
@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);
}
}
Then in one of the inner classes, I created an object of NotificationService
@Slf4j
@Component
@StepScope
public class SomeInnerClass implements Tasklet {
@Autowired
NotificationService notificationService;
// some code
When I run the application, it gives me this error:
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]
Note: If I test this directly in Notification library by creating an object of NotificationService, it works just fine! The issue is when it is being imported in other project.
答案1
得分: 1
你需要将 notificationService.url
属性添加到你的 SecondProject 的 application.yml 文件中。
英文:
You need add notificationService.url
property to your SecondProject's application.yml
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论