1

I have an application based on Spring Web model-view-controller (Spring MVC 3.2.8 ) and I want to resolve a placeholder

I have the file application.properties inside the folder /src/main/resources/config/

This is my class:

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

    ...
}

But I got this error running the app.

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

I also tried @PropertySource("classpath:/config/application.properties") with the same result

I also tried to put it in 1 of my configuration classes:

@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;
    }
}
La Carbonell
  • 1,976
  • 5
  • 22
  • 52

2 Answers2

0

Try this.

@PropertySource("classpath:/config/application.properties") in your confiuration class.
Sangam Belose
  • 4,262
  • 8
  • 26
  • 48
0

You should add @PropertySource in your configuration class like this

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();
    }

}

and not in the service class.

Also take note of the PropertySourcesPlaceHolderConfigurer Bean that is required when you want to inject properties using @Value("{}"). You may remove it as stated in comment if you don't wish to inject properties using @Value("{}") but wish to inject properties using the new Environment API.

This should solve your problem. You can learn more from here and here

Perry
  • 312
  • 1
  • 4
  • 12