解决Spring MVC 3.2.8中的占位符

时间:2017-07-11 09:43:33

标签: java spring spring-mvc property-placeholder

我有一个基于Spring Web模型 - 视图 - 控制器(Spring MVC 3.2.8)的应用程序,我想解决一个占位符

我在文件夹application.properties

中有文件/src/main/resources/config/

这是我的班级:

@Service("jobone")
@PropertySource("classpath:config/application.properties")
public class MyJobOne {

    private static final Logger LOGGER = Logger.getLogger   (MyJobOne.class);

    private File localDirectory = new File("tmpFtpFiles");

    private AbstractInboundFileSynchronizer<?> ftpInboundFileSynchronizer;

    @Autowired
    private SessionFactory myFtpSessionFactory;

    private boolean autoCreateLocalDirectory = true;

    private boolean deleteLocalFiles = true;

    private String fileNamePattern="*.*";


    @Value("${ftpRemoteDirectory}")
    private String remoteDirectory;

    ...
}

但我在运行该应用时遇到此错误。

Caused By: java.lang.IllegalArgumentException: Could not resolve placeholder 'ftpRemoteDirectory' in string value "${ftpRemoteDirectory}"

我也尝试@PropertySource("classpath:/config/application.properties")使用相同的结果

我还尝试将其放在我的一个配置类中:

@Configuration
@PropertySource("classpath:/config/application.properties")
public class FtpConfiguration {


    @Autowired
    private SessionFactory myFtpSessionFactory;

    @Bean
    @Scope(value="step")
    public FtpGetRemoteFilesTasklet myFtpGetRemoteFilesTasklet()
    {
        FtpGetRemoteFilesTasklet  ftpTasklet = new FtpGetRemoteFilesTasklet();
        ftpTasklet.setRetryIfNotFound(true);
        ftpTasklet.setDownloadFileAttempts(3);
        ftpTasklet.setRetryIntervalMilliseconds(10000);
        ftpTasklet.setFileNamePattern("README");
        //ftpTasklet.setFileNamePattern("TestFile");
        ftpTasklet.setRemoteDirectory("/");
        ftpTasklet.setLocalDirectory(new File(System.getProperty("java.io.tmpdir")));
        ftpTasklet.setSessionFactory(myFtpSessionFactory);

        return ftpTasklet;
    }

    @Bean   
    public SessionFactory myFtpSessionFactory()
    {
        DefaultFtpSessionFactory ftpSessionFactory = new DefaultFtpSessionFactory();
        ftpSessionFactory.setHost("la.mare.superiora");
        ftpSessionFactory.setClientMode(0);
        ftpSessionFactory.setFileType(0);
        ftpSessionFactory.setPort(1029);
        ftpSessionFactory.setUsername("carbonell");
        ftpSessionFactory.setPassword("nicinc");

        return ftpSessionFactory;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个。

@PropertySource("classpath:/config/application.properties") in your confiuration class.

答案 1 :(得分:0)

您应该在配置类中添加@PropertySource,如下所示

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource(value = { "classpath:/config/application.properties" })
public class AppConfig {

    /*
     * PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}") annotations.
     * Remove this bean if you are not using @Value annotations for injecting properties.
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

而不是服务类。

还要注意使用@Value("{}")注入属性时所需的PropertySourcesPlaceHolderConfigurer Bean。如果您不希望使用@Value("{}")注入属性但希望使用新的Environment API注入属性,则可以按照注释中的说明将其删除。

这应该可以解决您的问题。 您可以从herehere

了解详情