无法从异步方法获取应用程序属性


@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,要么是null。

该方法在将其制作异步之前就可以使用,并且在我不访问应用程序属性时,异步版本正常。

看起来问题是new SomeServiceAsync(),而不是@Autowired

我自己多次犯同样的错误。

最新更新