Announcement Announcement Module
Collapse
No announcement yet.
@Configuration, @PropertySource, @value and filesystem resources Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Configuration, @PropertySource, @value and filesystem resources

    hi

    I am new to Spring on the coding side, but have been working with spring based applications for a while on the operations front.

    I am having some trouble trying to externalise configuration injected using the @configuration and @propertysource annotations.

    "classpath:" incantations work, but trying to use "file:" fails

    here is the meat of the code.

    Code:
    ...
    
    @Configuration
    @PropertySource("file:/etc/product/database.properties")
    @EnableMongoRepositories("com.product.dao")
    public class SpringMongoConfiguration {
    
    	@Value("${mongo.url}")
    	private String mongoUrl;
        
    	@Value("${mongo.port}")
    	private String mongoPort;
        
    	@Value("${mongo.dbname}")
    	private String dbName;
    
    	@Value("${mongo.username}")
    	private String username;
    	
    	@Value("${mongo.password}")
    	private String password;
        
    	public @Bean MongoDoubleTypeIdConverter doubleTypeIdConverter() throws Exception {
    		return new MongoDoubleTypeIdConverter(mongoDbFactory(), new MongoMappingContext());
    	}
    
    	public @Bean static PropertySourcesPlaceholderConfigurer mongoPropertySourcesPlaceholderConfigurer() {
    		PropertySourcesPlaceholderConfigurer myConfigurer = new PropertySourcesPlaceholderConfigurer();
    		myConfigurer.setLocalOverride(true);
    		return myConfigurer;
    	}
    
    ...
    
    }
    in the application log, it appears to be working:

    Code:
    17:14:42.620 [pool-2-thread-1] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [URL [file:/etc/product/database.properties]] PropertySource with lowest search precedence

    ... but later ...

    Code:
    ... nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.data.mongodb.core.MongoOperations com.product.dao.FacebookRecordRepositoryImpl.mongoOperations; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springMongoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.product.config.SpringMongoConfiguration.mongoUrl; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'mongo.url' in string value [${mongo.url}]
    ... so the parameters are not being retained/ are not accessible when the configuration class is actually being instantiated.

    any pointers on this would be very helpful


    Nick
Working...
X