在 spring bean 中设置的 属性 的值在使用时变得空

The value to the property that is set in the spring bean is getting null when using

这是我的 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>

这是我的申请 class:

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" 未设置。我感到困惑的是,即使在我尝试给出直接值之后,它在调试时仍然显示为 null。我想知道哪里出了问题。我希望动态设置此 field/property 的值。感谢任何帮助。

正如 Angelo Immediata 在评论中提到的,我尝试使用 init-method 属性,现在我可以看到代码正在运行。我所做的更改:

<?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:/MY_QVC/fts.properties" />

    <bean id="shredFilesUnderTimePeriod" class="com.qvc.supplychain.app.delete.ShredFilesUnderTimePeriod" init-method="shredFilesFromDirectory">
        <property name="fileLocation" value="${LOCAL_FILE_DIR}/FileTransferIntegrationServices/ftpArchive"/>
    </bean>

</beans>

并且我删除了默认构造函数。

事实上,在您的情况下,此 Filelocation 将始终为 null,原因很简单,他们在 spring 文档

中提到了这一点

The Spring container validates the configuration of each bean as the container is created. However, the bean properties themselves are not set until the bean is actually created.

检查 this section

在您的 java 代码中,您在容器

设置它之前在构造函数中使用它 属性

你可以通过多种方式解决这个问题,其中之一是创建一个带有 String 参数的构造函数,并将 xml 中的 属性 更改为 construct-arg

 <constructor-arg name="fileLocation" value="${LOCAL_FILE_DIR}/FileTransferIntegrationServices/ftpArchive"></constructor-arg>

在构造函数中设置你的 属性 并使用它 - 你将它作为任何其他依赖项的必需项