【发布时间】:2020-10-13 18:36:32
【问题描述】:
我正在使用 Spring 启动 - 2.3.3.RELEASE。 application.yaml 中有一些值,我正在尝试使用 @Value 注释将其注入到类中。但由于某种原因,它们没有加载。结果应该是,在 SendPhoneByPhoneNumbers.java 中,我们应该能够从 application.yaml 中读取 notificationServiceURL。
注意-我使用的是工厂和策略模式。该项目将作为库,供其他项目导入和使用Service层暴露的方法。
这里是文件夹结构:https://imgur.com/a/jYr7wyP 我正在尝试通过在 Debug 模式下运行 Demo.java 来进行测试,以查看实际值的样子。
应用程序.yaml
notificationService:
url: "https://someURL.com"
Demo.java
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class Demo {
public static void main(String[] args) {
SpringApplication.run(Demo.class, args);
String title="Title";
String message="message";
List<String> phoneNumbers = new ArrayList<>();
phoneNumbers.add("333-222-1111");
PhoneService phoneService = new PhoneService();
phoneService.sendNotificationByPhoneNumbers(title, message, phoneNumbers);
}
}
PhoneService.java
@Service
public class PhoneService {
PhoneServiceImpl notificationServiceImpl = new PhoneServiceImpl();
public void sendNotificationByPhoneNumbers(String title, String message, List<String> phoneNumbers) {
notificationServiceImpl.sendNotificationByPhoneNumbers(title, message, phoneNumbers);
}
}
PhoneServiceImpl.java
@Slf4j
@Component
public class PhoneServiceImpl {
@Value("${notificationService.url}")
String url;
public void sendNotificationByPhoneNumbers(String title, String message, List<String> phoneNumbers) {
PhoneContext phoneContext = new PhoneContext(new SendPhoneByPhoneNumbers(url));
phoneContext.notify(title, message, phoneNumbers);
}
}
PhoneContext.java
public class PhoneContext {
private PhoneStrategy phoneStrategy;
public PhoneContext(PhoneStrategy phoneStrategy){
this.phoneStrategy = phoneStrategy;
}
public void notify(String title, String message, List<String> employees){
phoneStrategy.sendNotification(title, message, employees);
}
}
PhoneStrategy.java
public interface PhoneStrategy {
public void sendNotification(String title, String message, List<String> listOfEmployeeIdGroupNamePhoneNumbers);
}
SendPhoneByPhoneNumbers.java
@Slf4j
public class SendPhoneByPhoneNumbers implements PhoneStrategy {
RestTemplate restTemplate;
String notificationServiceURL;
BuildHttpRequest buildHttpRequest;
public SendPhoneByPhoneNumbers(String notificationServiceURL) {
this.notificationServiceURL = notificationServiceURL;
this.restTemplate = new RestTemplate();
this.buildHttpRequest = new BuildHttpRequest();
}
@Async
public void sendNotification(String title, String message, List<String> phoneNumbers) {
SmsMessage smsMessage= new SmsMessage(title, message, phoneNumbers, Collections.emptyList(), Collections.emptyList());
try {
HttpHeaders headers = new HttpHeaders();
headers.set("idToken", buildHttpRequest.getNewToken());
HttpEntity<SmsMessage> newRequest = new HttpEntity<>(smsMessage, headers);
restTemplate.postForObject(notificationServiceURL + "/someUrl", newRequest, String.class);
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
另外,如果你们对修改代码/结构以使其更好的方式有任何建议,请提出建议。
提前致谢。
【问题讨论】:
-
不要像“PhoneServiceImpl notificationServiceImpl = new PhoneServiceImpl();”那样实例化spring管理的bean。而是注入它们。如果这些 bean 由 spring 管理,Spring 将能够注入属性。
标签: java spring spring-boot