spring configuration with system property - spring

I have a question. Currently i start jboss with -P property that links to file with properties.
In this property file i have property - mongo.server.list=127.0.0.1. In Spring configuration i try to set this property as value of constructor of bean. But spring treat ${mongo.server.list} as value itself.
Here is the code
<bean id="systemPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
<bean id="mongo" class="com.mongodb.Mongo">
<constructor-arg index="0">
<value>${mongo.server.list}</value>
</constructor-arg>
</bean>

You should set your property file as a JVM property and read it in spring mvc configuration file as:
<context:property-placeholder location="file:///${-P}" />

You should specify the property as a JVM arg as -Dmongo.server.list=....

Did you try to start your server using -Dmongo.server.list=127.0.0.1 ? (i.e. using -D instead of -P, and no space between -D and the property name)

Related

Could not resolve placeholder in string value with external files

I have configured a config.xml file choosing the appropriate properties file depending on environment. I am running this as a Spring Boot app with Apache Camel.
The config looks like this.
<bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
<property name="locations" ref="locations" />
</bean>
<bean id="bridgePropertyPlaceholder"
class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="locations" ref="locations" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<beans profile="dev">
<util:list id="locations">
<value>classpath:config/users.properties</value>
<value>classpath:config/application.properties</value>
<value>classpath:config/application-dev.properties</value>
</util:list>
<beans profile="test">
<util:list id="locations">
<value>file:${project.dir}/config/users.properties</value>
<value>file:${project.dir}/config/application.properties</value>
</util:list>
When using the test profile I want to use the external files defined in the config (because I dont want to commit username/password to repo). That seems to work okay.
However, my users.properties file contains:
username=myusername
password=mypassword
and my application.properties contains:
loginParameters=username=${username}&password=${password}
when running java -jar myjar.jar --spring.profiles.active=test I encounter:
java.lang.IllegalArgumentException: Could not resolve placeholder 'username' in string value '${username}&password=${password}'
It clearly loads the properties files because it states:
Loading properties file from URL: file:...../users.properties
Loading properties file from URL: file:...../application.properties
Bridging Camel and Spring property placeholder configurer with id: bridgePropertyPlaceholder
...
And then the exception occurs. How can I resolve the issue where the application.properties file doesn't recognize my properties defined in users.properties? Everything works okay when running the dev-profile.
in your application properties, use $simple{username} and $simple{password} to tell Camel to put the values there.

How can I pass VM args to applicationContext in spring

How can I pass vm args (java -Dport = 5) to the application context?
I want to configure the port dynamically by the user, using the application context.
In case you trying to access the VM arguments in spring application context, then just add the below bean definition in context file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
</bean>
And access the VM args as ${vm_property}.
e.g if you passed the –Dport=5, which can be accessed as
<bean class="com.testing.test.MyClass">
<property name="myProperty" value="${port}"></property>
</bean>
I think There is only one way in spring using injection. and get the property value from any property file

Tomcat Context-Params ignored in Spring webapp when using PropertyPlaceholder

I was previously using, the now deprecated, class org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer to load a properties file from the server's filesystem. I had the following bean definied:
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="locations" value="${config}"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="false"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="searchSystemEnvironment" value="false"/>
</bean>
The config is an argument that is passed when starting Tomcat, i.e.
-Dconfig=/path/to/application.properties
For the webapp I also have a context file:
<Context docBase="/path/to/application.war">
<Parameter name="host" value="localhost" override="false"/>
<Parameter name="port" value="8080" override="false"/>
</Context>
If the .properties file, specified by the -Dconfig argument, contains the property that some other bean references then the value from the .properties file is used, otherwise the value from the the context xml file is used.
This allowed me to have a set of default properties deployed with the WAR and if required, I was able to specify a .properties file to override particular values.
Now, I'm updating to use the new property abstractions in Spring 3.1 but I can't seem to figure out what the equivalent approach to this is?
I have the same context file and war deployed in the same way, and I now have the following in the application:
<context:property-placeholder
location="${config}"
system-properties-mode="OVERRIDE"
ignore-resource-not-found="true"
ignore-unresolvable="true"/>
This finds and uses the properties from the properties file, BUT it does not use the values from the context XML file.
How do I get my application to use the context params when using this new property-placeholder?
Thanks.
To summarise the problem is that the Context Parameters from the servlet context file were not being used to resolve placeholders when using the new Property Placeholder namespace introduced in Spring 3.1.
I have figured out a solution, with the following
<context:property-placeholder location="${config}" local-override="true" ignore-resource-not-found="true"/>
I can specify one or more *.properties files on the local filesystem using a JVM arg, eg:
-Dconfig=/path/app.properties
If a placeholder property cannot be resolved after checking the app.properties file then the Servlet Context Parameters are checked.
This allows me to have default values using context params in a the web.xml and where I need to I can override these values by specifying the location of *.properties files using the config JVM arg.
The key to getting it to work this way was to include local-override="true", which is false by default. I'm not fully sure that it makes sense, since the description for that attribute is:
Specifies whether local properties override properties from files. Default
is "false": Properties from files override local defaults.
If the same property key exists in the app.properties and the web.xml the value from the app.properties is used.
Spring uses a default property file unless the user-based property file is defined. If you want to control .properties file, please follow the instructions posted here.
If you want to take advantage of application.properties there are two ways to do it.
<!-- allows for ${} replacement in the spring xml configuration from the
system.properties file on the classpath -->
<util:properties id="appProperties" location="classpath:application.properties"/>
<context:property-placeholder location="classpath:application.properties"/>
util tag lets you to use a Property class to read the properties across your application. For example:
#Autowired
public MyPropertyReader(Properties appProperties) {
String prop1 = appProperties.getProperty("my.address");
String prop2 = appProperties.getProperty("my.version");
}
If you want to use the values within your context file use the context:property-placeholder tag. Then you can use your values as
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="${jms.primary.server}"/>
where for example jms.primary.server=172.168.10.18:6161 in application.properties.

Spring issues with JVM Arguments

I have a normal plain servlet . I am trying to load property file from the file system using Spring ReloadableResourceBundleMessageSource class.The file location is supplied by the JVM arguments. The following is my declaration of my MessageSource bean
<bean id="xmlXpathProperties" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- check property file(s) every 600 second(10min) -->
<property name="cacheSeconds" value="600"/>
<property name="basenames">
<list>
<value>file:#{systemProperties.aircdrconfig}/cdr-airxml</value>
</list>
</property>
/bean>
If i am giving the JVM argument name with any special characters such as dots(.) or hyphen(-) for example : air.cdr.config, I am getting an exception like.
org.springframework.beans.factory.BeanExpressionException Field or property 'air' cannot be found on object of type 'java.util.Properties'
If i remove the dot symbol then it's working fine. Any idea to overcome this problem? Thanks in advance.
You will need to refer to the properties this way:
#{ systemProperties['air.cdr.config'] }

Spring PropertyPlaceholderConfigurer not replacing placeholder

I want to pass the WSDL url for an internal web service into my Spring beans.xml dynamically, using a PropertyPlaceHolderConfigurer.
Here's the scenario:
My web application is deployed in WebLogic 10.3.
The WSDL url is contained in a properties file that is located outside my application (directly under the corresponding domain folder, while my application is inside the autodeploy folder). I set the location of this properties file in my domain's setDomainEnv.cmd file like below:
set JAVA_PROPERTIES=%JAVA_PROPERTIES% %CLUSTER_PROPERTIES% -Dproperty.file.path.config=%DOMAIN_HOME%\Service.properties
This is what my Service.properties file contains:
Service.WSDL.PATH=http://localhost:8088/mockServiceSoap?WSDL
My Spring beans.xml configuration:----
<bean id="file.path" class="java.lang.System" factory-method="getProperty">
<constructor-arg index="0"><value>property.file.path.config</value></constructor-arg>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" ref="file.path"/>
</bean>
<bean id="myServiceId" class="com.test.service.ServiceImpl">
<property name="myServiceSoap">
<ref bean="myService"/>
</property>
</bean>
<bean id="myService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="com.test.service.ServiceSoap"/>
<property name="wsdlDocumentUrl" value="${Service.WSDL.PATH}"/>
</bean>
I enabled DEBUG log specifically for PPC and this is what I saw in my application log:
INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 178 - Loading properties file from URL [file:D:/bea10.3/user_projects/domains/my_domain/Service.properties]
So, it seems that although the Service.properties file is getting loaded by PPC, the ${Service.WSDL.PATH} is NOT getting replaced.
What am I doing wrong here?
Also, how can I find out if PPC tried replacing the value of the placeholder and with what value? I was hoping the log file would contain that info but there was nothing there.
Any help is appreciated.
I've figured out, that PropertyPlaceholderConfigurer needs to be declared first in the application Context file, otherwise there's no guarantee of the load order. It took me a few hours to realize this.
Try moving the "file.path" bean into the PropertyPlaceHolderConfigurer's location property.

Resources