Tuesday, April 13, 2010

Useful Spring Utilities(API's)

1. Default behaviour of Spring is that , when same names beans are loaded in a single applicationContext , the first bean will
be overridden by the latter.

We had usecase where we required to get an error when multiple beans had the same name.

I have to extend ClassPathXmlApplicationContext and overide this method as it was protected.

public void setAllowBeanDefinitionOverriding(
boolean allowBeanDefinitionOverriding) {

ctx.afterPropertiesSet(); //will initialize the context

2. To get teh number of beans initialized by the application Context.


3. To get the resouces read by created ApplicationContext based ona regular Expression.
[This is very handy in an integratde environment to exactlt know the resources ]

I had to extend ClassPathXmlApplicationContext as this method was protected.

protected Resource[] getConfigResources() {
// TODO Auto-generated method stub
return super.getConfigResources();

Resource[] resources = ((ResourcePatternResolver) ctx).getResources("classpath*:/META-INF/spring/conf*.xml");

where ctx is the initialized applicationContext.

4. If we need multiple definitons for <context:property-placeholder/>

Only one context:property-placeholder (the last one defined in the configuration files) will prevail.
You can define two separate PropertyPlaceholderConfigurers and work, only if the second one has a different placeholderPrefix and placeholderSuffix.

< bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name="location" value="classpath:/com/foo/2.properties" / >
< property name="placeholderPrefix" value="#[" / >
< property name="placeholdersuffix" value="]" / >
< / bean >

< bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name="location" value="classpath:/1.properties" / >
< / bean >

Normal way of replacing is ${property1} and with new prefix and suffix #[]


Gautam said...

Cool! Keep posting tips like this.


Dharmesh said...

Good post...

Free Domain Names @ .co.nr!