【发布时间】:2018-02-02 16:54:55
【问题描述】:
这是我的 beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="file:/C:/local/fts.properties" />
<bean id="shredFilesUnderTimePeriod" class="com.qvc.supplychain.app.delete.ShredFilesUnderTimePeriod">
<property name="fileLocation" value="${LOCAL_FILE_DIR}/FileTransferIntegrationServices/ftpArchive"/>
</bean>
</beans>
这是我的应用程序类:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
public class ShredFilesUnderTimePeriod {
public Logger logger = Logger.getLogger(this.getClass());
private final static int DAYS_LIMIT = 2;
private String fileLocation;
private static long currentTimeInMillis;
public ShredFilesUnderTimePeriod() {
shredFilesFromDirectory();
}
private void shredFilesFromDirectory() {
logger.info("Deleting the obsolete files");
currentTimeInMillis = Calendar.getInstance().getTimeInMillis();
Date currentDate = new Date(currentTimeInMillis);
logger.info("Today's date: " + "" + currentDate + "\n");
try {
File loadFilesFromTheDirectory = new File(fileLocation);
if (!loadFilesFromTheDirectory.isDirectory()) {
throw new FileNotFoundException("Is not a directory");
} else {
for (File file : loadFilesFromTheDirectory.listFiles()) {
if (file.isDirectory()) {
for (File subDirectoryFile : file.listFiles()) {
deleteFile(subDirectoryFile);
}
} else {
deleteFile(file);
}
}
logger.info("Obsolete files deletion got completed");
}
} catch (FileNotFoundException eMsg) {
eMsg.printStackTrace();
} catch (Exception eMsg) {
logger.error("Error while shredding obsolete files. Cause: " + eMsg.getStackTrace());
System.out.println("Error while shredding obsolete files. Cause: " + eMsg.getStackTrace());
} finally {
//System.exit(0);
}
}
private void deleteFile(File file) {
long totalNumberOfDays = 0L;
Date fileCreatedDate = null;
try {
if (file.isDirectory()) {
throw new FileNotFoundException("Is not a directory");
}
fileCreatedDate = new Date(file.lastModified());
logger.info(file.getName() + " file is created or last modified on: " + fileCreatedDate
+ ", total number of days present: " + totalNumberOfDays);
totalNumberOfDays = TimeUnit.DAYS.convert(currentTimeInMillis - fileCreatedDate.getTime(),
TimeUnit.MILLISECONDS);
if (totalNumberOfDays > DAYS_LIMIT) {
file.delete();
}
} catch (FileNotFoundException eMsg) {
eMsg.printStackTrace();
} catch (Exception eMsg) {
logger.error("Error while shredding obsolete files. Cause: " + eMsg.getStackTrace());
}
}
public String getFileLocation() {
return fileLocation;
}
public void setFileLocation(String fileLocation) {
this.fileLocation = fileLocation;
}
}
现在的问题是 fts.properties 文件正在加载,但属性的值:“fileLocation”未设置。我很困惑,即使在我尝试给出直接值之后,它在调试时仍然显示为空。我想知道哪里出了问题。我希望动态设置此字段/属性的值。任何帮助表示赞赏。
【问题讨论】:
-
属性在构造函数调用之后设置。因此,您不能在构造函数中调用该方法并认为填充了 fileLocation。您应该在属性集之后创建一个初始化方法
-
非常感谢您。它正在工作!
-
尝试在设置器文件位置上使用 Autowired 注释。并使用 Value annotation 传递文件路径。
应该在配置文件中使用。
标签: xml spring properties javabeans