0

For following class:

@Configuration
@PropertySource("classpath:META-INF/mongodb.properties")
public class MongoConfiguration {

    @Value("${mongo.username}")
    private String mongoUsername;

    @Value("${mongo.password}")
    private String mongoPassword;

    @Value("${mongo.url}")
    private String mongoUrl;

    @Value("${mongo.port}")
    private String mongoPort;

    @Value("${mongo.databasename}")
    private String mongoDatabaseName;

    @Bean
    public UserCredentials mongoCredentials() throws Exception {
        UserCredentials credentials = new UserCredentials(mongoUsername, mongoPassword);
        return credentials;
    }

    @Bean
    public MongoClient mongoClient() throws Exception {
        MongoClient mongo = new MongoClient(mongoUrl, Integer.valueOf(mongoPort));
        return mongo;
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoClient(), mongoDatabaseName, mongoCredentials());
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
       return new PropertySourcesPlaceholderConfigurer();
    }

}

I am getting following exception:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mongo.username' in string value "${mongo.username}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:255)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:749)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:740)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 77 more

I am using spring 3.2.0.RELEASE. Am I doing anything wrong?

** EDIT **

Here is my mongodb.properties file:

mongo.username=x
mongo.password=y
mongo.url=localhost
mongo.port=27017
mongo.databasename=articles
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • 1
    There is no `mongo.username` property in your properties file? Also why not simply make `mongoPort` and `int` instead of `String` spring can and will do the conversion for you. – M. Deinum Apr 30 '15 at 22:39
  • Try this @PropertySource(value={"classpath:META-INF/mongodb.properties"}) possible duplicate of http://stackoverflow.com/questions/28307237/spring-could-not-resolve-placeholder – Bacteria Apr 30 '15 at 23:23

0 Answers0