Announcement Announcement Module
Collapse
No announcement yet.
Environment.getProperty not returning property configured in .properties file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Environment.getProperty not returning property configured in .properties file

    I have a properties file called indexClassConfig.properties located under META-INF/spring/. I am able to use the @Value annotation on a class member in order to retrieve a value from this properties file. However, if I try to retrieve a property using Spring's Environment abstraction, the keys are not found, but I know they exist. It seems the only PropertySources registered are the systemProperties and systemEnvironment.

    This is what's contained in my indexClassConfig.properties file.
    Code:
    configuredIndexClassNames=DSNDOCS
    
    configuredKeyValues.DSNDOCS.DOCUTYPE=AUTH,BOL,CSTMS,DAMAGE RPT,INVOICE,LETTER OF ASSIGNMENT,LOAD CONFIRMATION,LUMPER,MISC,POD,POD_BOL,SCALE,WAIT
    In an @Configuration file, I have a String annotated with @Value("${configuredIndexClassNames}") which allows me to determine the configured index class names that I then lookup up in a database. That works just fine. The second line in the file shows configured key values for a key field named DOCUTYPE for the index class DSNDOCS.

    This is an excerpt from a class where I attempt to lookup the value of "configuredKeyValues.DSNDOCS.DOCUTYPE" from the properties file. The IndexClass and KeyField instance are passed to findConfiguredKeyValues, which is where the lookup for the value happens. Environment is injected into this class, but the property isn't found.
    Code:
    	@Inject
    	private Environment environment;
    	
    	/**
    	 * For the given IndexClass and KeyField, attempt to locate it's configured key values, if they exist.  Otherwise
    	 * returns an empty array.
    	 * 
    	 * @param indexClass The IndexClass to look in.
    	 * @param keyField The KeyField to look for.
    	 * @return
    	 */
    	public String[] findConfiguredKeyValues(IndexClass indexClass, KeyField keyField) {
    		List<String> configuredKeyValues = new ArrayList<String>();
    		
    		String property = buildPropertyKey(indexClass, keyField);
    		
    		if(environment.containsProperty(property)) {
    			for(String value : environment.getProperty(property, "").split(",")) {
    				if(value.length() > 0)
    					configuredKeyValues.add(value);
    			}
    		}
    		
    		return configuredKeyValues.toArray(new String[0]);
    	}
    	
    	/**
    	 * Build a property name to lookup in the Environment.
    	 * 
    	 * @param indexClass The IndexClass to retrieve the name from.
    	 * @param keyField The KeyField to retrieve the name from.
    	 * @return
    	 */
    	private String buildPropertyKey(IndexClass indexClass, KeyField keyField) {
    		return new StringBuilder("configuredKeyValues.")
    			.append(indexClass.getName()).append(".")
    			.append(keyField.getName()).toString();
    	}
    Last edited by pgrimard; May 9th, 2012, 12:25 PM.

  • #2
    I'm experiencing the same problem with Spring 3.1.1

    The PropertySourcesPlaceholderConfigurer is defined in XML. The @Value works fine but not the Environment. The Environment is autowired but I cannot get my properties from it.

    XML
    Code:
    <bean id="propertyConfigurer"          
              class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"
              p:locations="/WEB-INF/properties/*.properties"
              p:ignoreUnresolvablePlaceholders="true" />

    Code:
    @Autowired
    private Environment env;
    @Value( "${db.username}" )
    String test;
    The variable 'test' can get the correct value but the env.getProperties("db.username") returns null.

    Any ideas? Many thanks!

    Comment

    Working...
    X