ehcache.xml can't use the properties file property - java

I define a system.properties in my class path, then I do like this in spring configuration xml:
<context:property-placeholder location="classpath*:/system.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />
I aslo defind ehcache like this :
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/ehcache.xml" />
<property name="shared" value="true" />
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
I define a properties in system.properties file :
system.project_name=myproject
ehcache.xml like this :
<diskStore path="e:/${system.project_name}/cache" />
I want use the system.project_name property to store the cache, but when I deploy my project, I find the Directory is:
that is say I could't use the properties even I define a PropertyPlaceholderConfigurer in Spring configuration xml file?

Related

Modifying EHCache beans defined in Spring

Assuming the belowcache definition in Spring, is it possible to set bootstrapCacheLoaderFactory and cacheEventListenerFactory through a separate ehcache.xml file or in the same xml file? If we set them in the Cache definitions in ehcache.xml, which cache definitions(in the current xml file or ehcache.xml ?!) will be applied?
Where can I set cacheManagerPeerProviderFactory and cacheManagerPeerListenerFactory for CacheManager?
<bean id="reportCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager"/>
<property name="maxElementsInMemory" value="${cache.report.maxMemoryElements}"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="WEB-INF/ehcache.xml"/>
</bean>
Would the below tag in ehcache.xml work?
<cache name="reporteCache">
<bootstrapCacheLoaderFactory class = "net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties = "bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"/>
<cacheEventListenerFactory class = "net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, asynchronousReplicationIntervalMillis=100"/>
</cache>
Both of the xml and java code would be fine.
Thank you in advance.
Just in case someone else is reading this? You can set the cacheManagerPeerListenerFactory in your ehcache.xml file as follows
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.2,
multicastGroupPort=4455, timeToLive=1"/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="host=hostname,port=40001, socketTimeoutMillis=2000"/>

Two beans with same name in different contexts

I have the following beans in my contexs:
<!-- Context 1 -->
<beans profile="ldap">
<bean id="activeDirectoryAuthProvider" class="com.production.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
</beans>
<!-- Context 2 -->
<bean id="activeDirectoryAuthProvider" class="com.test.TestActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
My goal is to use the first bean only for production version another one for test purposes.
Namely when I start test based on production context I expect that production bean would be replaced by test bean with needed configuration.
But unfortunately when I tried to create two beans with same name only production bean is created and another one is ignored. Another thing that I noticed that when I tried to change test bean name to: activeDirectoryAuthProvider1 then both beans are successfully created. Can anyone explain why it happen and suggest possible solution how it can be bypassed?
You need to use different contexts for development and production. In each context you define only the relevant bean (i.e. only 1 bean with a certain name). If you use maven you can put the test/development context under src/test/resources and the production context under src/main/resources
If you do not use maven there are other approaches. You can find an example here: http://mrhaki.blogspot.it/2009/02/use-spring-configurator-to-support.html
Take a look at Spring Profiles you can have one for test and one for prod.
<beans profile="test">
<!-- Context 1 -->
<bean id="activeDirectoryAuthProvider" class="com.production.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
</beans>
<beans profile="prod">
<!-- Context 2 -->
<bean id="activeDirectoryAuthProvider" class="com.test.TestActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
</beans>
You can set the active profile in a various ways. Check the docs.

MethodInvokingFactoryBean didn't work

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.

Spring loading PropertySourcesPlaceholderConfigurer from JBoss context

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.

Change spring bean alias with System property

I try to figure out whether it's possible to change a spring alias configuration through a system property.
That's the configuration:
<beans>
<bean id="beanOne" ... />
<bean id="beanTwo" ... />
<bean id="beanThree" ... />
<alias name="beanOne" alias="beanToUse" />
<bean id="consumer" ...>
<constructor-arg ref="beanToUse" />
</bean>
</beans>
I'd like to be able to use a JVM property e.g. with -Duse=beanThree to select another bean for the alias.
Unfortunately using the straight forward solution <alias name="#{systemProperties.use}" alias="beanToUse" /> throws a NoSuchBeanDefinitionException exception :(
Any suggestions?
Did you try to use spring 3.1 profiles?
<beans>
<bean id="beanOne" ... />
<bean id="beanTwo" ... />
<bean id="beanThree" ... />
<beans profile="A">
<alias name="beanOne" alias="beanToUse" />
</beans>
<beans profile="B">
<alias name="beanTwo" alias="beanToUse" />
</beans>
<bean id="consumer" ...>
<constructor-arg ref="beanToUse" />
</bean>
</beans>
and choose through system property -Dspring.profiles.active=A. I haven't tried aliases in profiles but you could just have alternative beanToUse definitions in each profile:
<beans>
<beans profile="A">
<bean id="beanToUse" ... defined as beanOne ... />
</beans>
<beans profile="B">
<bean id="beanToUse" ... defined as beanTwo .../>
</beans>
<bean id="consumer" ...>
<constructor-arg ref="beanToUse" />
</bean>
</beans>
Here's another way to do this using SpEL.
I have two implementations of DataStrategy type with bean ids testDataStrategy and realDataStrategy
I can choose between the beans by setting the property 'data.strategy' in the Property file in my Java project.
<bean id="myBeanId" class="com.some.path.MyBeanClass" >
<property name="dataStrategy" value="# {'${data.strategy}'.equalsIgnoreCase('TEST_DATA') ? testDataStrategy : realDataStrategy}" />
</bean>

Categories

Resources