Java Web Project Class Path - spring

I am using Eclipse Helios. I have a Dynamic Web Project.
I want to load a property file using Spring 3.1.0 for which I use the following configuration
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:resources/dc-config.properties</value>
</property>
</bean>
This folder by name resources is present in WEB-INF/classes directory
But when I try to start my Tomcat 6 Server I get the following error
Caused by: java.util.MissingResourceException: Can't find bundle for base name dc-config, locale en_US
Isin't my resources folder in the classpath as it is in the classes folder which in turn is in the classpath ?
Please let me know if I am missing something here

wrong bean class maybe ?
Try this:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:resources/dc-config</value>
</list>
</property>
</bean>

Maybe the problem is a small typo. Try either
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"> <!-- not locations -->
<value>classpath:resources/dc-config.properties</value>
</property>
</bean>
or
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:resources/dc-config.properties</value>
</list>
</property>
</bean>

Related

Reading JNDI name from external properties file in Spring

I am developing a Spring web application where I am using JMS as well as some datasource connection.
Now Instead of hardcoding the JNDI names of DataSource/Jms Connection Factory,I want to read them from a external properties file.
I used the following configuration::
<bean id="myProps" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath*:myFile"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
`<jee:jndi-lookup` id="dataSource" jndi-name="${DS_JNDI}" expected-type="javax.sql.DataSource"/>
But during deployment time it is throwing an error in weblogic:::
javax.naming.NameNotFoundException: Unable to resolve '${DS_JNDI}'. Resolved ''; remaining name '${DS_JNDI}'
Is it like that I cannot put a property file entry when using <jee:jndi-lookup>???
you should remove the star after classpath, and add properties of file extension
<bean id="myProps" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:myFile.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:myFile.properties</value>
</list>
</property>
</bean>
This is the correct solution of the problem.I think from Spring5.x onwards it has stopped appending ".properties" extension.

how to load different spring properties for different environment

I have two properties files. The main.properties file has a field defined
environment=DEV
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${catalina.home}/conf/main.properties</value>
<value>WEB-INF/project.${environment}.properties</value>
</list>
</property>
However, I got this:
Could not open ServletContext resource [/WEB-INF/project.${environment}.properties]
Does any one know how to fix it?

Spring+ Hibernate :How to map single session factory in multiple modules (deployed as jar)

I am working on Spring + Struts2 and Hibernate currently, My requirement is :
I have Master module which will make DB connection, shared by all other modules (deployed as Jar) in the system.
All Module specific .hbm and persistence classes will exists in Module itself
So for example My master module will have Hibernate connection file (through spring) will all .hbm mapping files, Below is the sample of Hibernate connection made through Spring.
<bean id="dataSourceErik" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="testWhileIdle" value="true" />
<property name="minEvictableIdleTimeMillis" value="120000" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
</bean>
<bean id="sessionFactoryErik"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceErik" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.generate_statistics=true
hibernate.show_sql=false
hibernate.jdbc.batch_size=10
hibernate.bytecode.use_reflection_optimizer=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
net.sf.ehcache.configurationResourceName=configuration/ehcache.xml
</value>
</property>
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/Abc.hbm.xml</value>
<value>classpath:configuration/hibernate/Xyz.hbm.xml</value>
</list>
</property>
</bean>
<bean id="AbcActionDAO" class="au.com.master.persistance.dao.AbcDbSession">
<constructor-arg ref="sessionFactoryErik" />
</bean>
<bean id="XyzActionTypeDAO"class="au.com.master.persistance.dao.XyzDbSession">
<constructor-arg ref="sessionFactoryErik" />
</bean>
if i add below code in above xml i can access '''sessionFactory''' and can connect with the DB. as i am giving the path of deployed subModule jar file, see below code :
<property name="mappingJarLocations">
<list>
<value>WEB-INF/lib/subModule.jar</value>
</list>
</property>
All above .hbm and DAO/DbSession classes exists in Master module. Now i want to use this '''sessionFactoryErik''' in my Sub modules deployed as jar. so for that i created another xml file in Sub module which will take reference of above '''sessionFactoryErik''' and will have mappings of this module specific .hbm and DAO/DbSession. Refer below code:
<bean id="sessionFactoryMonitor" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="sessionFactoryErik" ref="sessionFactoryErik" />
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/DDDType.hbm.xml</value>
</list>
</property>
</bean>
<bean id="testActionDAO" class="au.com.java.subModule.persistance.dao.DddActionDbSession">
<constructor-arg ref="sessionFactoryMonitor" />
</bean>
if i deployed the project and reboot the server i am getting below error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryMonitor' defined in URL [jar:file:/home/developer/Project/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/webapps/erik/WEB-INF/lib/erik-monitor-1.0-SNAPSHOT.jar!/configuration/spring-monitor-dao.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactoryErik' of bean class [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Bean property 'sessionFactoryErik' is not writable or has an invalid setter method.
Can anyone help me for this. How can i get the same session object in sub module as only Master module can make connection as it will not have any knowledge of its deployed (deployed as jar) module.
Thanks.
Tapan
the class org.springframework.orm.hibernate4.LocalSessionFactoryBean doesn't have a property called sessionFactoryErik. I am guessing that you are injecting your datasource to LocalSessionFactoryBean. if so Change this
<bean id="sessionFactoryMonitor" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="sessionFactoryErik" ref="sessionFactoryErik" />
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/DDDType.hbm.xml</value>
</list>
</property>
</bean>
to this
<bean id="sessionFactoryMonitor" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="sessionFactoryErik" />
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/DDDType.hbm.xml</value>
</list>
</property>
</bean>
sessionFactoryErik bean returns org.hibernate.internal.SessionFactoryImpl object,
so you should specify org.hibernate.internal.SessionFactoryImpl or its super class
(SessionFactory) type in au.com.master.persistance.dao.XyzDbSession constructor
reference.

Read property file outside war using spring

enter code hereI have a property file placed in the etc folder. "myapplication.properties" and few other property files in each sub module.. i am try to do the following
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>${config}</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
I am trying to do mvn -Dconfig=~/my.properties jetty:run
The properties are read from application.properties but not for config..
While running application i get the ${jdbc.url} not correct .. This url is present in my.properties ..
How can this be achieved ?
Thanks
This is what I had, to run it
<bean id="placeholderConfigConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${config}" />
</bean>
And add -Dconfig=/var//my.properties in the MAVEN_OPTS.. and did mvn jetty:run
Another one line solution I found.. instead of making verbose configuration just do
<context:property-placeholder location="file:${config}"/>
I think this feature becomes available in spring 3.1 via the new Environment abstraction. See the following spring blog for details:
http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/.
If spring 3.1 is not an option you can hard-code the filename and path in the spring xml configuration file to some well-known location and then developers can symlink against it.

Loading .properties file in a jar from my web-app

I have created a JAR that I need to use in my WEB-APP. Both are created with spring framework. I would like to load a .properties file outside the JAR file, in the main context of the web-application. And I want to do it with the facilities that Spring offers us.
I've tried to do something like this in my spring.xml file inside the JAR:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/my.properties</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="myJob" />
<property name="cronExpression" value="${my.cronExpression}"/>
</bean>
</property>
</bean>
Trying to load my.cronExpression from my.properties file. But without any success.
I always get this error:
Could not resolve placeholder 'my.cronExpression'.
I've tried to change the location with many variants, using classpath:/WEB-INF/classes/my.properties etc...
But I'm not able to load the configuration file.
Thanks for your help.
Use classpath:my.properties - /WEB-INF/classes is root of your classpath.
Try declaring it as follows:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/my.properties</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
</bean>
I have gone through your code and want you to try this code snippet
It works well for me :)
<bean id="placeholderProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/WEB-INF/classes/my.properties" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1" />
</bean>

Resources