I have a spring 3.1 application loading settings using PropertySourcesPlaceholderConfigurer and I want to manage test and production environments simply loading settings from server context overriding settings specified in local file properties.
Next example works fine with Tomcat, how can I do the same in JBoss AS 7.1?
In spring context I have:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="localOverride" value="false" />
<property name="locations">
<list>
<value>classpath:Application.properties</value>
<value>classpath:ApplicationTest.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
In ApplicationTest.properties (that overrides Application.properties):
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:#XXXX:1521:xxxx
jdbc.username=myUsername
jdbc.password=myPassword
In context.xml (in Tomcat/conf directory):
<Context>
...
<Parameter name="jdbc.driverClassName" value="oracle.jdbc.OracleDriver" override="false"/>
<Parameter name="jdbc.url" value="jdbc:oracle:thin:#XXXX:1521:ProductionSID" override="false"/>
<Parameter name="jdbc.username" value="myProductionUsername" override="false"/>
<Parameter name="jdbc.password" value="myProductionPassword" override="false"/>
...
</Context>
In this way all parameters specified in context.xml overrides parameters in ApplicationTest.properties.
Is there a way to specify context parameter in JBoss? (i.e. in standalone.xml file)
EDIT - SOLVED:
As suggested in the answers, if I put entries in WEB-INF/jboss-web.xml inside webapp that works:
<jboss-web>
...
<env-entry>
<env-entry-name>jdbc.username</env-entry-name>
<env-entry-value>myUsername</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
...
</jboss-web>
But my goal was to put the config file out of webapp.
I solved in this way, I load the config file from $SERVER/conf directory:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="localOverride" value="false" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:Application.properties</value>
<value>classpath:ApplicationTEST.properties</value>
<value>file:${catalina.home}\conf\ApplicationPRODUCTION.properties</value><!-- FOR TOMCAT -->
<value>file:${jboss.server.base.dir}\configuration\ApplicationPRODUCTION.properties</value><!-- FOR JBOSS-->
</list>
</property>
</bean>
If someone knows how to put Parameters in JBOSS config out of webapp (as TOMCAT) is well appreciated!
Thanks All!
According to the JBoss7 documentation:
In AS7 the file context.xml is ignored. Most of the old context.xml
configuration has been moved to jboss-web.xml.
See here for more details.
In older versions you could add these properties to context.xml as usual - JBoss use Tomcat as a servlet container.
Use a jboss-web.xml file. This file specifically replaces context.xml from Tomcat.
Related
I want to use simple-spring-memcached to cache data. so using this https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started for my purpose.
i add in my dispatcher-servlet.xml
<aop:aspectj-autoproxy />
<import resource="simplesm-context.xml" />
<bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
<property name="caches">
<set>
<bean class="com.google.code.ssm.spring.SSMCache">
<constructor-arg name="cache" index="0" ref="defaultCache"/>
<!-- 5 minutes -->
<constructor-arg name="expiration" index="1" value="0"/>
<!-- #CacheEvict(..., "allEntries" = true) doesn't work -->
<constructor-arg name="allowClear" index="2" value="false"/>
</bean>
</set>
</property>
</bean>
<bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
<property name="cacheName" value="defaultCache"/>
<property name="cacheClientFactory">
<bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl"/>
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="127.0.0.1:11211"/>
</bean>
</property>
<property name="configuration">
<bean class="com.google.code.ssm.providers.CacheConfiguration">
<property name="consistentHashing" value="true"/>
</bean>
</property>
</bean>
i also add simple-spring-memcached-3.5.0.jar and spymemcache.jar,spymemcache-provider.jar to lib folder. but when i run my project occur this exception:
Cannot find class **[net.nelz.simplesm.config.MemcachedClientFactory]** for bean with name 'memcachedClientFactory' defined in ServletContext resource [/WEB-INF/simplesm-context.xml]; nested exception is java.lang.ClassNotFoundException: net.nelz.simplesm.config.MemcachedClientFactory
you must add lib for this class.
It seams that you have also an old version of Simple Spring Memcached on your classpath. The class net.nelz.simplesm.config.MemcachedClientFactory is no longer available in 3.x. Check your classpath and remove all Simple Spring Memcached artifacts older than 3.5.0.
I have 2 maven modules - Webapp and Persistence. Persistence module has a property file - database.properties and two context files - persistenceContext.xml (main), datasourceContext.xml (imported to persistenceContext.xml).
persistenceContext.xml
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.example.persistence"/>
<context:property-placeholder location="classpath:database.properties, classpath:application.properties" />
<import resource="classpath:datasourceContext.xml" />
Files persistenceContext.xml, datasourceContext.xml and database.properties are in directory resources of Persistence module.
datasourceContext.xml
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="${database.url}"/>
<property name="user" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="debugUnreturnedConnectionStackTraces" value="true"/>
<property name="unreturnedConnectionTimeout" value="20"/>
<property name="minPoolSize" value="5"/>
<property name="initialPoolSize" value="10"/>
<property name="maxPoolSize" value="50"/>
<property name="maxStatements" value="50"/>
<property name="idleConnectionTestPeriod" value="120"/>
<property name="maxIdleTime" value="1200"/>
</bean>
database.properties
database.url=jdbc:mysql://255.255.255.255:3306/...
database.username=...
database.password=...
Persistence maven module is dependency of Webapp module. When I deploy Webapp to Tomcat it failed with exception:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [spring-datasource.xml]: Could not resolve placeholder 'database.url' in string value "${database.url}"
I tried the following solutions:
moved database.properties under directory path resources\com\example\persistence.
use property-placeholder or PropertyPlaceholderConfigurer in datasourceContext.xml instead of using property-placeholder in persistenceContext.xml.
moved database.properties under Webapp module and then try point 1.
I always convinced that database.properties file is under classpath of Persistence module.
Nothing from mentioned doesn't work, I looked also on other threads on stackoverflow and spring forum - but with no success.
I create a properties file contain system properties in classpath, the name is system.properties
the file like :
system.project_name=springsilkworm
I also use EHcache, the ehcache.xml like:
<diskStore path="${java.io.tmpdir}/${system.project_name}/cache" />
I want use the system.project_name define in system.properties。
I developed my project based on Spring, so I created applicationContext.xml like:
<context:property-placeholder location="classpath*:/system.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="setProperty" />
<property name="arguments">
<list>
<value>system.project_name</value>
<value>${system.project_name}</value>
</list>
</property>
</bean>
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/ehcache.xml" />
<property name="shared" value="true" />
</bean>
but when I start my app, I find the folder which EHcahce use like :
that was say, the MethodInvokingFactoryBean didn't work, I also get the property in my code with System.getProperty("system.project_name") code,I got the result of “null”, why? I couldn't find out the wrong.
in my applicationcontext.xml i have this :
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dashboardsupervisor" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
here i am connecting with my database :
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
MySQLRdbHelper rdbHelper = (MySQLRdbHelper)
ctx.getBean("ManagerSupervisor");
What is want is to NOT read the password "1234" from my applicationcontext.xml
and read it from some properties file in my local drive .
as this will be running on different machines and every one have different passwords.
Can i achieve this .
thanks
Yes, you can, and the key to this is Springs PropertyPlaceholderConfigurer.
For example, you create a file on your file system called database.properties, containing your password (note, that you can also add more settings to this file, like username, JDBC url, etc).
jdbc.password=1234
Next, you need to declare a PropertyPlaceholderConfigurer bean and point it to the database.properties file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>path/to/database.properties</value>
</property>
</bean>
Note that the path is interpreted as a classpath resource, unless it is prefixed with file:.
Finally, replace the configuration of your dataSource bean: replace
<property name="password" value="1234" />
with
<property name="password" value="${jdbc.password}" />
You could use profiles:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev1");
ctx.load("*Context.xml");
ctx.refresh();
<bean id="database1" profile="dev1"/>
<bean id="database2" profile="dev2">
The best way is to create the data source in application server and configure as below in application.xml
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>YourDSName</value></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
.....
</bean>
I want to use jndi in my spring mvc application for 2 things:
datasource settings
store the location of my properties file so I can do a lookup in my code
I actually have 2 applications, 1 is a spring mvc and the other is a spring so I'm planning on using the same jndi settings for both.
Also, the 2 applications run on different contains (tomcat and jetty), I've never used jndi before so I hope I can define this in a single location and have both applications point to the same jndi file (assuming its a file).
So as for #1, my datasource settings in my spring context file are currently:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/mydb_development"/>
<property name="username" value="devuser1"/>
<property name="password" value="123"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="testOnBorrow" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
Can someone tell me how I can extract this out to use JNDI, confused, is it a separate file that has to be in my class loader, or do I hard code the path in my spring context file?
And as for #2, in my code I am currently loading a properties file that is in my class path like:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream("test.properties");
Now the test.properties file is in my classpath so it works out of the box, but what I want to know/understand is if I could somehow use jndi to lookup the location of the properties file (if that makes sense?) and not have this file in my classpath potentially.
I did google around and I know in spring I can pull in jndi using:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/mysqlDataSource" />
But what I don't understand is, where is the actual file that has the settings? Can it be in a location other than my web.xml file? (my other application again is using spring, but it is a daemon so it doesn't have a web.xml file).
The goal for me is to be able to change the values of the jndi file (like username, passwords, file paths) and NOT have these embedded into a jar. I want to be able to manually edit these files on the production server or dev servers and just restart the container.
I'm kinda rusty with spring but I remember using a PropertyPlaceHolderConfigurer, something like:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound">
<value>true</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
<property name="locations">
<list>
<value>file:path to your file
</value>
<value>file:path to another file if needed
</value>
</list>
</property>
</bean>
Then you can use the values on the .properties defined on the list directly. i.e. if you have a property like
db.driver=com.mysql.jdbc.Driver
you can use it like
bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
when you want to define a value.
If you want further control over the file, the path to the file itself can be registered on your server and you can look it up through JNDI, changing
file:path to your file
into
file:${propertiesFilePath}
I'm pretty sure you can look up those configuration properties by code too by a normal JNDI lookup.