繁体   English   中英

在Spring MVC 3.2.8中解析占位符

[英]Resolving placeholders in Spring MVC 3.2.8

我有一个基于Spring Web model-view-controller(Spring MVC 3.2.8)的应用程序,我想解析一个占位符

我在文件夹/src/main/resources/config/有文件application.properties

这是我的课:

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

尝试这个。

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

您应该像这样在配置类中添加@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注入属性,则可以按照注释中的说明将其删除。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM