I am able to get some property using injection. But when I use spring Environment, I cannot find it. My final goal is it find all defined properties and search between them by prefix. None of answers in Spring: access all Environment properties as a Map or Properties object worked for me.
This is my Dispatcher
configuration:
<bean id="proxyProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:device.properties</value>
<value>file:/${user.dir}/device-local.properties</value>
<value>file:${smartpos.config.dir:/etc/pos}/device.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="properties" ref="proxyProperties"/>
<property name="placeholderPrefix" value="!device{"/>
<property name="placeholderSuffix" value="}"/>
</bean>
Injection with fixed value works fine:
@Service
public class DeviceService {
@Value(value = "!device{application.version.pattern}")
public void setVersion(String apiVersion) throws DeviceAPIException { }
Looking for the property in Environment
does not work, I cannot locate it:
@Autowired
public DeviceService(Environment env) {
String s = env.getProperty("application.version.pattern");
s = env.getProperty("!device{application.version.pattern}");
}
I have opened the Environment
and there were 5 property sources like System or JNDI but none of them contained my properties from device.properties
file. This may be the reason why no answer from linked question work. They all list system properties but my properties are missing.
I run this application in jetty using jetty-maven-plugin.