我正在嘗試使用@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'。 –