我有一个通知库(用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 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库中对此进行测试,则效果很好!问题是何时将其导入其他项目中。
答案 0 :(得分:1)
您需要将notificationService.url
属性添加到SecondProject的应用程序中。yml