无法从异步方法获取应用程序属性
Can't get application properties from async method
@Service
public Class SomeService {
private SomeServiceAsync someServiceAsync = new SomeServiceAsync();
...
public String DoAThing() {
CompletableFurute<String> future = someServiceAsync.GetAString();
return future.get();
}
}
@Service
@EnableAsync
public Class SomeServiceAsync {
@Value("${someProp1}")
private String someProp1;
@Autowired
private Environment env;
...
@Async
public CompletableFuture<String> GetAString() {
System.out.println(someProp1); // returns null
String someProp2 env.getProperty("someProp2"); // throws null pointer exception
return CompletableFurute.completedFuture("blablabla");
}
}
我的问题很简单,在异步执行某些方法后我无法访问我的应用程序属性 运行。在我尝试执行该方法并从 @Value 获取 null 或 env 为 null 之前,没有任何失败。
该方法在使其异步之前有效,当我不访问应用程序属性时,异步版本工作正常。
看起来问题出在 new SomeServiceAsync()
,而不是 @Autowired
。
我自己也犯过很多次同样的错误。
@Service
public Class SomeService {
private SomeServiceAsync someServiceAsync = new SomeServiceAsync();
...
public String DoAThing() {
CompletableFurute<String> future = someServiceAsync.GetAString();
return future.get();
}
}
@Service
@EnableAsync
public Class SomeServiceAsync {
@Value("${someProp1}")
private String someProp1;
@Autowired
private Environment env;
...
@Async
public CompletableFuture<String> GetAString() {
System.out.println(someProp1); // returns null
String someProp2 env.getProperty("someProp2"); // throws null pointer exception
return CompletableFurute.completedFuture("blablabla");
}
}
我的问题很简单,在异步执行某些方法后我无法访问我的应用程序属性 运行。在我尝试执行该方法并从 @Value 获取 null 或 env 为 null 之前,没有任何失败。
该方法在使其异步之前有效,当我不访问应用程序属性时,异步版本工作正常。
看起来问题出在 new SomeServiceAsync()
,而不是 @Autowired
。
我自己也犯过很多次同样的错误。