How to use context-param defined in web.xml in dispatcher servlet? - spring

I have a very small concern, to which I couldn't find an answer even after Googling for quite sometime.
How do we use context-param defined in web.xml in the dispatcher servlet?
In my web.xml, I have defined it like this:
<context-param>
<param-name>root.path</param-name>
<param-value>/root</param-value>
</context-param>
I can access it in my Java class using the below lines of code:
String rootPath = sce.getServletContext().getInitParameter("root.path");
However, I don't see a way to use this value in dispatcher servlet. I want to use a variable for /root in the below case than the value itself.
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="file:/root/main.properties" />
Any help is highly appreciated.
Thank you.

Try using
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="file:/#{contextParameters.root.path}/main.properties" />
This will create problem if we give name as "root.path", instead give name as "root_path"

Related

Can we replace Springframework annotations (#CacheConfig, #Cacheable, #CachePut) in the XMl file?

I am implementing a module with Spring Cache mechanism. The module is generic and can cache different type of entities. So I don't want to change the Java code and want the user to configure the applicationcontext.xml file accordingly. He can put the name of the different types of entities within the applicationcontext.xml and the code should work. For e.g. -
<context:annotation-config/>
<cache:annotation-driven cache-manager="cacheManager"/>
<context:component-scan base-package="com.nokia.oss.sure.adapter"/>
<bean id="NetworkEntityService" class="com.nokia.oss.sure.adapter.cache.NetworkEntityServiceImpl"/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="NetworkEntity"/>
</set>
</property>
</bean>
He may change NetworkEntity to ServiceEntity and so on.
So in the Java code I need to mention -
#CacheConfig(cacheNames={"NetworkEntity"})
Or I can put the same for every method -
#CachePut(cacheNames="NetworkEntity", key="#entity.sureName")
public Entity addEntity(Entity entity) {
return entity;
}
But as I stated earlier, I don't want to put the cache name "NetworkEntity" in the Java code, but want to put the same in the applicationcontext.xml file. Is it possible?
Furthermore is it possible to omit all the annotations in the Java file? If I just use AbstractApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); is it possible to mention in the applicationContext.xml file what are the methods where I want to apply the #Cacheable annotation for e.g.
I searched a lot, couldn't find it anywhere.
Thanks
Nirmalya
I found out the answer. We can put the following in the applicationContext.xml -
<!-- define caching behavior -->
<cache:advice id="cacheAdviceInterface" cache-manager="cacheManager">
<cache:caching cache="NetworkEntity">
<cache:cacheable method="getEntity"/>
<cache:cache-put method="putEntity"/>
</cache:caching>
</cache:advice>
In that case we don't need to put the #CacheConfig, #CachePut etc annotations within the Java file.

Reading property file in java using Spring

Have been trying to understand how to load a property file using Spring in java and populate java.util.Properties object.
Use this newly created property object as a constructor arguement for another bean.
Appreciate any pointers on this.
Thanks,
cabear
To load a properties file in Spring use the PropertiesFactoryBean to make it easier to use there is a properties tag in the util namespace you can use.
<util:properties id="props" location="location-of.properties" />
Then you have a bean named props which you can use as any regular bean.
<bean id="otherBean" class="my.app.MyClass" >
<constructor-arg ref="props" />
</bean>
When also using the property placeholder support you could use the same properties object again instead of loading it again, by referencing it using the properties-ref attribute of the property-placeholder tag.
<context:property-placeholder properties-ref="props" />
Here is how you read a properties file :
#Value("${memberaddedsubject}") // id of the content you want to read.
private String memberAddedSubject; // Taking content in String class.
To use this property file, you have to instruct spring, this way :
<util:properties id="props" location="your-location-to-props" />
Add the above line in your applicationContext.xml or the XML file you have(not web.xml)
I have put my properties file in /projectname/resources/.
I hope this is what you are looking for, if not, let me know.

Spring Configuration Query

I have one spring configuration file with entry like below...
<bean id="beanId" class="a.b.c.d.MyBean">
<property name="firstProperty" value="report_{date}.xls"/>
</bean>
Somewhere in my java code, I am fetching this bean and then its property "firstProperty" later.
I am little curious, when I get the value of property "firstProperty" I get report_.xls i.e report_20130307.xls
I have searched all my code including bundles, xmls but not clear that where we are setting {date} with todays timestamp.
Do you have any clue where we can do this?
Thanks
Jai
It is the property-placeholder mechanism.
Read more on http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-context-pphc.
In most of the cases, the values to property are set from properties file using expression language. Like
<bean id="dataSource" class="a.b.c.d.DataSource">
<property name="databaseUrl" value="{db.url}"/>
</bean>
Or if the property is a ref to another bean, e.g. Object B is member variable of Object A.
<bean id="refA" class="a.b.c.d.A">
<property name="b" ref="refB"/>
</bean>
<bean id="refB" class="a.b.c.d.B">
</bean>
Its quite simple guys...as we know setter are called for each property. So same in my case,
In bean we are setting variable "firstProperty" + today timestamp like below.
public void setfirstProperty(String firstProperty) {
this.firstProperty = firstProperty + <methodToReplaceDateStringWithTimeStamp>;
}
Thanks
Jai

disable spring autowire

i ahave an app with struts2.2 and spring 3.1 and i want to disable spring autowire.
I googled a little bit and found that i have to put at <beans> tab default-autowire="no", but this doesn't seems to work.
Then i fount that i can declare this for every <bean> tag like this : <bean autowire="no">, but this does not seems to work either.
When i enabled spring debug logger i can see a lot aof messages like this :
INFO: DEBUG [http-thread-pool-8080(3)] (ConstructorResolver.java:739) - Autowiring by type from bean name 'com.common.actions.PopupAction' via constructor to bean named 'intermedService'
and the corresponding entry in applicationConfig.xml is :
<beans default-autowire="no"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="PopupAction" scope="prototype" class="com.common.actions.PopupAction" autowire="no">
<constructor-arg type="com.common.services.abs.iIntermedService" ref="intermedService"/>
<constructor-arg type="com.common.services.abs.iLocationService" ref="locationService"/>
<constructor-arg type="com.common.services.abs.iUserService" ref="userService"/>
<constructor-arg type="com.common.services.abs.iPhoneService" ref="phoneService"/>
</bean>
why does spring trying to autowire this action as long as i defined the dependency by hand here and i defined auto-wire="no"?
Or this message tells me that the wiring was made by type via constructor(as i wanted) and "Autowiring by type" means that from the 4 params he matched intermedService with my variable intermed service by type (and not by order or something else)?
The Struts 2 Spring plugin sets autowiring to "name" by default. Currently I don't believe the plugin allows "none" as a value, but you could try using "auto"--I suspect this will not help, as it is still an autowiring bean factory.
This has been brought up before; I don't recall any real resolution. I've brought it up again to see if it can be addressed in the next release where we doing some significant cleanup.
Edit There's also the struts.objectFactory.spring.autoWire.alwaysRespect constant which defaults to false; try setting it to true. I don't recall which sense of the boolean means which, or if it has the effect--looking in to it now.
A way to get around it (until it gets fixed) is to just name your fields/constructor arguments and your beans differently, so that spring can't match them.
I believe default-autowire="no" is enabled by default . i.e, if you don't specify default-autowire, it means default-autowire="no" . Try setting autowire-candidate="false" if you don't want this bean to be autowired http://static.springsource.org/spring/docs/2.5.x/reference/beans.html

c3p0 useScatteredAquireTask Spring Bean

Hello I wan't to know how can I set up a bean so that it sets the ScatteredAquireTask to "True".
I've been trying:
<bean id="c3p0Props" class="com.mchange.v2.resourcepool.BasicResourcePool.ScatteredAcquireTask" >
<property name="USE_SCATTTERED_ACQUIRE_TASK" value="true" />
</bean>
I also tried ...resourcepool.experimental.useScatteredAcquireTask... didn't worked. I'm not sure how can I set this on spring. I'm using 0.9.1.2, can't go to 0.9.2.prep1 at the moment. Thanks.
That's because USE_SCATTTERED_ACQUIRE_TASK isn't a property of the ScatteredAcquireTask class (i.e. there's no method called setUSE_SCATTTERED_ACQUIRE_TASK), it's an internal static field of the class that's not accessible to Spring.
You're not going to be able to set that values in a Spring bean defintion, you need to find out how to influence that value by some other means.

Resources