Introduction to Spring (part2)

In this LDS Java Stack training we will build on what we learned in the Introduction to Spring training. We will be discussing some development productivity concepts such as xml configuration extensions (namespace handlers) and annotations.




Xml Configuration Extension (Namespace Handlers)

  • Spring bean lifecycle
  • What they provide
  • How they work
  • Handlers that Spring provides

JSR 250 (Common) Annotations

  • Bean Lifecycle
  • @PostConstruct
  • Annotations / Bean Post Processors

Spring Annotations

  • Component scanning
  • Stereotype annotations
  • @Scope

JSR 330 (DI) Annotations

  • @Named / @Inject

Slide Deck


Xml Schema-based Configuration Presentation

Get Adobe Flash player

Lab 1 XML Schema-based Configuration

Create a reusable List of Rabbits using the Spring util xml schema-based configuration

Documentation can be found here:

  • Add a util namespace to your bean definition file
    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=""
    <!-- definitions here -->
  • Define the list with the appropriate namespace prefix
   <util:list id="rabbitList">
  • Copy the values from the anonymous list in the rabbits property of the Farm definition to your util:list
    <util:list id="rabbitList">
        <bean class="">
            <constructor-arg name="weight" value="10.5" />
        <bean class="">
            <constructor-arg name="weight" value="2.25" />
        <bean class="">
            <constructor-arg name="weight" value="5.0" />
  • Inject the util:list into the Farm by id
    <property name="rabbits" ref="rabbitList" />

Lab 1 Solution

Get Adobe Flash player

Bean Initialization Presentation

Get Adobe Flash player

Lab 2 Bean Initialization

Create an init method using @PostConstruct

  • In the LDSTech IDE add the following inside of the properties element of your pom.xml (Maven defaults to Java 5 and these annotations are not directly include in Java until Java 6)

Then right click on your project and select Maven -> Update Project Configurations

  • Create an init method (that prints something out) on your Farm object
  • Annotate the method with @PostConstruct
  • Run your tester to see if the init method was called (it shouldn’t have been)
  • Modify your bean definition file to contain the following:
    <beans xmlns="" 	

        <context:annotation-config />

Lab 2 Soution

Get Adobe Flash player

Spring Component Annotations Presentation

Get Adobe Flash player

Lab 3 Spring Component Annotations

Change SomeBean to be configured by annotations only

  • Make sure you are loading your SomeBean with the following: SomeBean someBean = context.getBean(SomeBean.class);, or if you are loading it by id please change the id to match the default id of someBean
  • Comment out or remove the SomeBean configuration <bean id="someBean" ... /> in the bean definition file
  • Annotate SomeBean with @Component
  • Also annotate it with @Scope("prototype") just for the fun of it
  • Add the component-scan element to the bean definition file
    <beans xmlns="" 	
	xsi:schemaLocation=" "> 

	<context:component-scan base-package="" />
* Run your tester to make sure that SomeBean is still registered and prints its message

Lab 3 Solution

Get Adobe Flash player

DI Annotations (JSR 330) Presentation

Get Adobe Flash player

Lab 4 DI Annotations (JSR 330)

Convert your farm to be configured via annotations instead of xml

  • Add the jsr-330 dependency to your pom.xml
  • Add the component scanner to the bean definition file
  • The component scanner contains the annotation-config configurations as well, so that can be removed
  • Remove the farm definition from the xml bean definition file
  • Annotate the farm object with @Component

How do you handle the initialization of the list of rabbits?

  • Annotate the rabbit list with @Resource(name="rabbitList") (you might expect @Inject @Named("rabbitList") to inject the list appropriately but it does not). We felt that @Resource was enough of an edge case that we did not cover it, but it is a JSR 250 annotation that will find and inject a resource of a given name/id.

If you have not already done so, in your application context (bean definition) xml file, set the name of the rabbit with scope protoype to be prototypeRabbit and the name of the rabbit with scope singleton to be singletonRabbit or something similar to distinguish them.

    <property name="name" value="Singleton Rabbit" />
    <property name="name" value="Prototype Rabbit" />
  • Add a Rabbit prizeRabbit attribute if you don’t already have one, and inject your prototypeRabbit
    public class Farm {
        private Rabbit prizeRabbit;

Can you remove the prototypeRabbit and singletonRabbit declarations from the definition file and handle them in annotations?

  • Add a Chicken class as follows:
    import org.springframework.stereotype.Component;

    public class Chicken {
	private String name = "Chicken Licken";

	public String getName() {
		return name;
	public void setName(String name) { = name;
  • Add a farm animals method as follows
    public void printFarmAnimals(Chicken chicken, Rabbit singletonRabbit) {
        System.out.println("Printing the inject farm animals *****************");

	for (Rabbit rabbit : rabbits) {
  • Annotate this method with @Inject, and annotate the singletonRabbit parameter with @Named("singletonRabbit") specifying the id of your singleton rabbit
  • Then run your SpringTester class

Lab 4 Solution

Get Adobe Flash player
This page was last modified on 20 April 2011, at 12:45.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.