Spring context:annotation-config

In our previous examples we have to define a bean for each BeanPostProcessor we wanted to use.

Spring provides a shortcut to do this in the context namespace.

We simply need to add the context namespace to our declared namespaces at the top of our config xml file and add the context:annotation-config to our bean definitions as follows:


<beans xmlns="http://www.springframework.org/schema/beans"

<!-- Person Beans -->
<bean id="jondoe" class="com.skills421.examples.spring.model.Student">
<property name="name" value="Jon Doe" />
<property name="email" value="jon.doe@gmail.com" />
<property name="studentId" value="DoeJ-001" />

<!-- Address Beans -->
<bean id="london" class="com.skills421.examples.spring.model.Address">
<property name="addressLines">
<value>House of Commons</value>
<value>Parliament Square</value>
<property name="town" value="London" />
<property name="county" value="Greater London" />
<property name="postcode" value="SW1 1AA" />

<bean id="manchester" class="com.skills421.examples.spring.model.Address">
<constructor-arg index="0">
<value>14 Town Plaza</value>
<value>Inner Township</value>
<constructor-arg index="1" value="Manchester" />
<constructor-arg index="2" value="Greater Manchester" />
<constructor-arg index="3" value="MC15 1AA" />

<!-- 	<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /> -->
<!-- 	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> -->

<context:annotation-config />

With these two simple changes, we no longer need to define our BeanPostProcessors.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s