Introduction to Spring Continued

Introduction To Spring (part2)



In this 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 Configuration Extensions Presentation

(Not Yet Available)

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

Bean Initialization (JSR 250 Common Annotations) Presentation

(Not Yet Available)

Lab 2 JSR 250 Annotations

Create an init method using @PostConstruct

  • In the LDSTech IDE add the following properties to your pom.xml (for some reason the IDE appears to be defaulting to Java 5 and these annotations are not directly include in Java until Java 6)
  • 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="" 	
	xsi:schemaLocation=" "> 

        <context:annotation-config />

Spring Component Annotations Presentation

(Not Yet Available)

Lab 3 Spring Component Annotations

Change SomeBean to be configured by annotations only

  • 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

DI Annotations (JSR 330) Presentation

(Not Yet Available)

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
  • 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?

  • 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?

This page was last modified on 5 April 2016, at 14:51.

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