我创建了一个ApplicationScoped
bean,它有一个PostConstruct
名为 start 的方法。每当我想FacesContext
在 start 方法中获取实例并返回时null
:
@ManagedBean
@ApplicationScoped
public class RemoveOldFilesScheduler implements Serializable {
@PostConstruct
private void start() {
final FacesContext facesContext = FacesContext.getCurrentInstance();
if(facesContext != null) {
String realDownloadDirName = facesContext.getExternalContext().getRealPath("/") + DOWNLOAD_DIRECTORY;
File downloadDir = new File(realDownloadDirName);
if (downloadDir.exists()) {
removeOldFiles(downloadDir.listFiles());
}
}
}
在这种情况下我该如何访问facesContext
?
我想在 start 方法中获取我的下载目录的真实路径,但我不知道如何在不使用FaceContext
.
还有另一种方法吗?