Spring MVC putting values into XML Config file - java

I am working a Spring MVC project and in my Service object I need some information like system password, id, url etc but I would like to put this into one of the XML files so it can be changes without changing code.. which XML should I put it in and how do I read it into the object

Moving constants to XML is a first step, but to make your application truly configurable you should use external .properties file:
<context:property-placeholder location="file:///foo/bar/conf.properties" />
And then use it everywhere in your XML configuration:
<property name="password" value="${db_password}"/>
Where conf.properties contains:
db_password=secret
Note that you can also place properties file inside WAR (with location="classpath:/foo/bar/conf.properties").
If you are a happy user of Spring 3.1 (currently RC2) you can take advantage of new #PropertySourceannotation:
#Configuration
#PropertySource("classpath:/com/myco/app.properties")

Related

How to directly read application.property value into another configuration xml file in spring boot

I have specified some webservice endpoints into applicaiton.properties file as below
application.properties
config.middleware.soap.service.endpoint.sample=http://xxx.xxx/sample/
Now i want to directly use those values into another configuration file that is in my case root-context.xml file for creating soap class using jax-ws client. but the property is never understood by spring boot if i refer it from applicaiton.properties value. why not? if i directly provide the endpoint it works. what is the simplest way to use application.properties file values into anther configuration file?
root-context.xml
<jaxws:client id="sampleClient" serviceClass="com.sample.wsdl.sample"
address="${config.middleware.soap.service.endpoint.sample}">
...
</jaxws:client>
in my case both the root-context and application.properties file reside in src/main/resources folder.. so i assume both the files gets loaded on classpath when the applcation boot strap.
It worked finally when i used it in below mentioned way
<jaxws:client id="acctInqClient" serviceClass="com.ge.india.capital.wsdl.spine.AcctInq"
address="#{environment['config.middleware.soap.service.endpoint.sample']}">
provided, i declared one property in the name config.middleware.soap.service.endpoint.sample in the applicaiton.properties file.
But i would like to know why only ${config.middleware.soap.service.endpoint.sample} didn't work. thank you.

Get Spring Environment as Properties

I wonder if there is a way to extract properties from Spring Environment (e.g. obtained from ApplicationContext) in the form of Properties instance? Or, at least, is there a way to enumerate the properties in Spring Environment, or get them all as a map, or any other way I can turn a [initially unknown] set of properties into a Properties object?
I need this in order to create a jclouds Context by calling org.jclouds.ContextBuilder.newBuilder() and .overrides(Properties). The idea is to configure the actual cloud provider solely by means of .properties file, and I don't want to couple application logic with provider-specific properties.
[UPDATE]
The .properties files to be used are configured using <context:property-placeholder>, and it actually specifies a list of paths, like this:
< context:property-placeholder location=
"classpath:/jdbc.properties,
file:${jboss.server.config.dir}/jdbc.properties,
file:${catalina.home}/conf/jdbc.properties"
ignore-resource-not-found="true"/>
which suggests that the .properties file is searched in the mentioned list of locations in order. I would like to achieve the following:
keep the list of .properties files and their possible locations in this XML definition file only;
allow to place jclouds related properties in any of the .properties files mentioned in the XML;
access the properties, resolved and loaded by Spring, in the form of Properties object so I am able to feed that to jclouds ContextBuilder.
Please let me know if all of this is feasible. Thank you in advance!
-Vlad
If you wan't to use properties in your Spring configuration then you can simply use:
<context:property-placeholder location="classpath:foo.properties" />
To get the properties in your code later you can simply read this file from the classpath into a Properties object:
props.load(MyClass.class.getClassLoader().getResourceAsStream("foo.properties"));
Alternatively you can have a look at PropertyPlaceholderConfigurer.
UPDATE
Updated after Deinum's remark but only if you are getting the properties from a Spring managed bean:
<util:properties id="myProps"
location="classpath:foo.properties"/>
<context:property-placeholder properties-ref="myProps" />
Now you can inject myProps into Spring managed beans (no need to load them again from the classpath).
You could use PropertiesFactoryBean and do something like this:
<bean id="jcloudsProps"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>your/props/file.properties</value>
</property>
</bean>
You can then use jcloudsProps as you would any other Spring bean.

How to load a property file just once and use it throughout the application in java?

I have a scenario where i need to show the success/error messages in jsp's from the controller. The controller has access to many methods and each method may return some message. These messages are stored as key-value pair in a property file which i need to access. Now i want to load this property file just once and use it through out the application. How can this be achieved? the framework is spring mvc. Presently i am doing something like this in every class but this approach doesn't seem right. Please help!
Properties prop = new Properties();
prop.load(getClass().getClassLoader().getResourceAsStream(fileName+".properties"));
What you are looking for is probably what is described in this blog post.
In short, here is what you have to do:
First tell spring where the properties file in your spring configuration.
That will look something like:
<context:property-placeholder location="classpath:application.properties"/>
Second you inject the properties into Spring beans with the #Value annotation
you can load the properties file using spring configurations as below:
<context:property-placeholder location="classpath:application.properties"/>
Add the above configuration in the spring beans configuration file and put the properties file under the source folder which are by default in the classpath.
#Value annotation can be used for injecting the property values at your controller. Something like this:
#Value("${name}")
private String name;
your properties file entries would look like this:
enter code herename=your name
I hope this explanation would solve your problems.

Working with properties files outside war with Spring

I am working on a Spring 3.0.5 web application that accesses LDAP and two databases. I have a properties with configuration information for the LDAP server and that databases, in applicationContext-security.xml and dispatcher-servlet.xml, but I would like to make it so each server can have different data properties without changing a file in the WAR. Can I somehow put a file somewhere else on the server and still access it from within my application?
Add this to your context
<context:property-placeholder location="${envfile}"/>
This will load the properties file located at ${envfile}, a variable you can set with Java's startup paramater like this
-Denvfile="file:/var/server/environment.properties"
Or maybe in Tomcat's startup script
CATALINA_OPTS=" -Denvfile=file:/var/server/environment.properties"
Values can be retrieved in your controllers using Springs Value annotation like this:
#Values("${myvalue}")
private String myValue;
Please note that these features require Spring 3.1, more information here
Good luck!
Try
<util:properties id="props" location="file:///path/to/server.properties"/>

Web App with Spring - Using properties in Service

I want to load few values from a proporties files that is in the /WEB-INF/ folder.
I usually use this in my xml file when I develop a software using WebServices
<util:properties id="configProperties" location="classpath:/WEB-INF/config.properties" />
and then access to the value in Java using:
#Value("#{configProperties['clientURL']}")
private String clientURL;
public String urlClient() {
return clientURL;
}
But it doesn't work on my webapp, it's always returning the null value.
WEB-INF is not on the classpath. The classpath starts from WEB-INF/classes/. So I would advise to place the properties file there (and change the location property accordingly). The service layer should not know that it serves a web application (which has WEB-INF)
I found the solution to my problem here:
http://codingbone.wordpress.com/2010/02/28/how-to-load-properties-files-into-spring-and-expose-to-the-java-classes/

Categories

Resources