0
我在加載服務中使用@PostConstruct註釋的方法中的資源文件時遇到困難。在PostConstruct方法中加載資源
@Service
public class MyService {
@Autowired
ResourceLoader resourceLoader;
//@Autowired
//private ApplicationContext applicationContext;
@PostConstruct
public void initialize() {
Resource res = resourceLoader.getResource("classpath*:myResource.csv");
//Resource res = applicationContext.getResource("classpath*:myResource.csv");
... use loaded resource ...
}
}
我一直收到錯誤IllegalStateException: The resources may not be accessed if they are not currently started
。
我哪裏錯了?它是否與bean初始化的順序有關?
@PostConstruct在bean完全初始化後調用,所以理想情況下ResourceLoader應該被注入。也許,Servlet版本問題?看看這裏發佈的答案:http://stackoverflow.com/a/29453024/1969874 –