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:

student1.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 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" />
</bean>

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

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

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

<context:annotation-config />
</beans>

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

Advertisement

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