View RSS Feed

Spring Framework

Wiring Bean Post Processor in Spring

Rate this Entry
by , 11-16-2011 at 04:32 PM (2612 Views)
In the last post, we create a class AnsiConvertor that implements the BeanPostProcessor interface. Now if we want to use this class in our application is running within a bean factory, we need to programmatically register the BeanPostProcessor using the factory’s addBeanPostProcessor() method:

Java Code:
BeanPostProcessor convertAnsi = new ConvertAnsi(); 
factory.addBeanPostProcessor(convertAnsi);
Or if you decide to use an application context, you’ll need to register the post processor as a bean within the context.

Java Code:
<bean id=" convertAnsi" class="com.acme.springexamples.domain.ConvertAnsi"/>
The container will recognize the convertAnsi bean as a BeanPostProcessor and call its post-processing methods before and after each bean is initialized. So using the convertAnsi bean, all String properties of all beans will be converted to UTF-8. For example, suppose you had the following bean defined in XML:

Java Code:
<bean id="car" class="com.acme.springexamples.domain.Car"> 
<property name="description">
<value>Ford Fiesta</value> 
</property>
</bean>
When the “convertAnsi” processor is finished, the description property will ensure that the text is all UTF-8.

Submit "Wiring Bean Post Processor in Spring" to Facebook Submit "Wiring Bean Post Processor in Spring" to Digg Submit "Wiring Bean Post Processor in Spring" to del.icio.us Submit "Wiring Bean Post Processor in Spring" to StumbleUpon Submit "Wiring Bean Post Processor in Spring" to Google

Categories
Bean , Configuration , XML

Comments