How to externalize configuration in spring? - java

I am attempting to externalize the configurations using spring, but cannot get it to work properly..
Here is what I did so far:
create a property file inside the war file (src/test/resources/) for each environment.
For example: nonprod-key.properties & prod-key.properties with content like so:
key.name=NameOfPrivateKey.pfx
key.password=JustAPasswordForPrivateKey
Then in my jboss-cxf.xml, I would like to read the above value as follows:
<import resource="#{systemProperties['environment']}-key.properties" />
<http:conduit name="*.http-conduit">
<http:tlsClientParameters
secureSocketProtocol="SSL">
<sec:keyManagers keyPassword="${key.password}">
<sec:keyStore type="PKCS12" password="${key.password}" resource="${key.name}" />
</sec:keyManagers>
... ... ...
</http:tlsClientParameters>
</http:conduit>
And then in eclipse, run configurations --> Arguments --> VM Arguments
-Denvironment=nonprod
Unfortunately, the above does not work. :(
I am getting this error message:
class path resource [#{systemProperties['environment']}-key.properties] cannot be opened because it does not exist
I was attempting to use the suggestion from here :
http://forum.springsource.org/showthread.php?98988-Access-external-properties-file-from-SPRING-context-file&p=332278#post332278
But cannot seem to get it to work. What am I doing wrong?
Could someone please give an example/sample of how best to do accomplish this.
Thank you.
-SGB

I believe one needs to be on Spring 3.1.x to use profiles. We are not ... yet.
Anyways, the final solution that seems to work for us is to use :
<context:property-placeholder location="classpath:${environment}-key.properties"/>
instead of
<import resource="#{systemProperties['environment']}-key.properties" />
Everything else is same as listed in my original post (question).
Hope someone finds this useful.
SGB

You can use Property Place Holder. If you want a flexible configuration, eg. a default configuration stored in your war which can be overrided by an external configuration, you can use directly the PropertyPlaceholderConfigurer bean like :
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:ignoreResourceNotFound="true">
<property name="locations">
<array>
<bean class="org.springframework.core.io.ClassPathResource" c:path="${environment}-key.properties"/>
<bean class="org.springframework.core.io.FileSystemResource" c:path="relative/path"/>
</array>
</property>
</bean>
path attributes can used SPEL for example to reference property or system env variable.
Have a look to this article and this how to read System environment variable in Spring applicationContext

Related

java.lang.ClassNotFoundException When Filepath is Correct

I'm working with Spring right now and have been trying to resolve a number of Failed to load ApplicationContext errors. This latest one is because it can't find a specific class when creating a bean structured like the following:
<bean id="problematicBean"
class="com.domain.application.search.problematicBean">
<property name="missingClass">
<bean class="com.domain.application.search.MissingClass">
</bean>
</property>
</bean>
I've triple checked and the filepath used in the bean class is correct. Other beans with identical paths (at least to the point of applicaiton) work perfectly. The file exists exactly where it should be for this classpath to work. The only difference is that this is a groovy file. Does anyone know why my application can't find the class?

gettign proxy of a bean in different project inside java class

I am new to the world of spring so i may ask a silly question but please let me the solution of my below problem please .
My problem is that I have two projects independent project nae is project A and project B ,now in project A i have the below xml configuration of bean
<bean id="abcService" class="com.jmx.JMXServiceImpl" autowire="no">
<constructor-arg index="0">
<ref bean="jobDetailsDomainHome" />
</constructor-arg>
</bean>
now in project A this bean get initilazied easily now i need this same bean initialized in project B also , so i have added project A in project B classpath also now please advise inside java class named rty of Project Bhow can i call this same bean abcService
The bean abcService depends on bean jobDetailsDomainHome. So there's no way to use abcService without the other bean.
You can split the configuration in various xml files. So define the abcService and the needed beans in one xml file, which is imported by the configurations of project A and project B.
<import resource="classpath*:service-context.xml" />
The import of xml files can use the classpath like shown above. But you can use locations in the file system too.
It's not important which bean is defined in which file as long as every needed bean is defined.

How to use JNDI DataSource provided by WebLogic 12.2.1 in Spring?

I created a JNDI connection with the following Values:
i selected Generic Data Source option
Name: jdbc/sampleDataSource
JNDI Name: jdbc/sampleDataSource
Spring Config File:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/sampleDataSource" />
I'm getting below error.
Error An error occurred during activation of changes, please see the log for details.
Error javax.naming.NameNotFoundException: While trying to lookup 'jdbc.sampleDataSource' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/sampleDataSource'
Error While trying to lookup 'jdbc.sampleDataSource' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/sampleDataSource'
I was unable to resolve it.
How do i configure in Spring 4.
Any addition jar file is required. Please help on this.
Sometimes this happens when you forget to Target your defined datasource to a specific server. You can find it in Weblogic's administration server :
then in the Targets tab:
you should select the target.
If that wasn't the problem, you may try the way of getting your datasource in the applicationContext.xml:
<bean id="dataSource" name="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/mcdsDS"/>
<property name="resourceRef" value="true"/>
</bean>
And use the dataSource reference wherever you need a datasource.
Hope this would be helpful.

Maven builds for different environments using xml templating - differences too complicated for simply properties or token replacement

I am trying to come up with a solution for managing XML configuration files for multi-environment builds that does not involve maintaining one configuration file for each environment. By XML configuration file, I mean XML files used at run-time, ie web.xml, and not the POM itself. I could of course maintain a separate XML file for each environment and then define a Maven property that contains the different file path and then have separate Maven profiles that point to the correct file path for the profile to correctly package them into the WAR/EAR/etc based upon environment, but I would prefer a different solution.
Like I am suggesting in the title, the differences are not something that can be accomplished by a simple token replacement - completely different XML structures are required in different environments. I originally tried using the maven Antrun plugin to run Ant's xmltask to add/remove/delete nodes via Xpath, but this is overly verbose and complicated to maintain, especially considering this plugin's inability to properly handle XML namespaces in a non-verbose manner.
Ideally, the XML file would like a normal template file, ie:
<x:if environment="production">
<a b="c">
<d>
</a>
</x:if>
<x:else>
<g />
</x:else>
At build time, ie during package or process-resources phases, the resultant XML file would contain only one set of XML or the other (in this example).
Note how, like I mentioned above, the node structures are completely different between environments.
Any ideas or suggestions?
There is "bean definition profiles".
Basic usage:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="businessService"
class="com.c...s.springthreeone.business.SimpleBusinessServiceImpl"/>
<beans profile="dev,qa">
<bean id="constructorBean"
class="com.gordondickens.springthreeone.SimpleBean"
c:myString="Constructor Set"/>
<bean id="setterBean"
class="com.gordondickens.springthreeone.SimpleBean">
<property name="myString" value="Setter Set"/>
</bean>
</beans>
<beans profile="prod">
<bean id="setterBean"
class="com.gordondickens.springthreeone.SimpleBean">
<property name="myString" value="Setter Set - in Production YO!"/>
</bean>
</beans>
</beans>
Add to your WEB.XML for selecting active:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>
Or jUnit Tests:
#ActiveProfiles(profiles = "dev")
Or you could set Environment Variable/JVM Parameter:
SPRING_PROFILES_DEFAULT=dev
Oryou could set selected profile with maven:
mvn -DSPRING_PROFILES_DEFAULT=dev
You can create basic maven project with common stuff and create two projects with different web.xml and other files.
Use maven web-app overlays function to marge resources, in that case you will have one project with common files and two other projects - each for required env.
You could even make these projects as modules of another project and run required depending on maven profile.

How to get hibernate mapping files from another project using xml based config?

I'm working with spring and hibernate. Currently I have the context config file like this
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- other properties -->
<property name="mappingDirectoryLocations" value="classpath:mappings/" />
</bean>
the *.hbm.xml mappings are in the same project.
Now I plan to pull some entities together with the mappings out, so they can be shared with other projects. The question is, how should I configure the sessionFactory bean to get *.hbm.xml files from the newly created project?
I tried mappingJarLocations but got error saying that the class path is not valid.
Instead of classpath: use classpath*:.
Check What is the difference between "classpath:" and "classpath:/" in Spring XML? for a extended answer on the differences between the 2.
AFASIK, Hibernate looks for mentioned hbm files in all the jars in classpath. You need to mention only the files.

Categories

Resources