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.
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.Code:configuredIndexClassNames=DSNDOCS configuredKeyValues.DSNDOCS.DOCUTYPE=AUTH,BOL,CSTMS,DAMAGE RPT,INVOICE,LETTER OF ASSIGNMENT,LOAD CONFIRMATION,LUMPER,MISC,POD,POD_BOL,SCALE,WAIT
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(); }


Reply With Quote