1

我有一个基于 Spring Web model-view-controller (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")同样的结果

我还尝试将其放入我的 1 个配置类中:

@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;
    }
}
4

2 回答 2

0

尝试这个。

@PropertySource("classpath:/config/application.properties") in your confiuration class.
于 2017-07-11T09:48:59.193 回答
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("{}"). 如果您不希望@Value("{}")使用新的 Environment API 注入属性但希望使用新的 Environment API 注入属性,则可以按照评论中的说明将其删除。

这应该可以解决您的问题。您可以从这里这里了解更多信息

于 2017-07-11T10:16:44.273 回答