我正在尝试使用@Value注释并自动填充属性文件中的变量,但没有运气。值未被设置并且为空。在构造对象之后发生Spring Boot - @Value返回null
taskService.java
@Service
public class TaskService {
@Value("${a}")
String aa;
public final RestTemplate restTemplate;
public TaskService(RestTemplateBuilder restTemplateBuilder){
System.out.println("----------xxxxxxxxxxx-------------" +aa);
this.restTemplate = restTemplateBuilder.build();
}
public Task getTask(int taskId) throws TaskDoesNotExistException{
try {
return this.restTemplate.getForObject("/tasks/{taskId}", Task.class,
taskId);
} catch (HttpClientErrorException e) {
if(e.getRawStatusCode() == 404)
throw new TaskDoesNotExistException("Task not found", e);
}
return null;
}
}
eventhandler.java
@Component
@RepositoryEventHandler(Application.class)
public class ApplicationRepositoryEventHandler {
@Autowired
TaskService taskService;
@HandleBeforeCreate
public void handleApplicationCreate(Application application) throws
TaskDoesNotExistException{
for (Integer taskId: application.getTasks()){
taskService.getTask(taskId);
}
}
}
请[编辑]你的问题,以显示你如何获得有此问题的'TaskService'实例。 – Kenster
嗨ochi,在哪里使用@Configuration注解..我已经尝试在服务类中,仍然返回空值 – user7700138
值构造函数完成后注入。只需在构造函数参数中添加@Value(“$ {a}”)String aa'。 –