LDSTechForumProjects

Introduction to Spring Continued

Introduction To Spring (part2)

Prerequisites

Outline

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.

Overview

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

Media:IntroductionToSpring(part2).pptx‎

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: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util

  • Add a util namespace to your bean definition file
    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <!-- definitions here -->
    </beans>
  • Define the list with the appropriate namespace prefix
   <util:list id="rabbitList">
   </util:list>
  • 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="org.lds.training.Rabbit">
            <constructor-arg name="weight" value="10.5" />
        </bean>
        <bean class="org.lds.training.Rabbit">
            <constructor-arg name="weight" value="2.25" />
        </bean>
        <bean class="org.lds.training.Rabbit">
            <constructor-arg name="weight" value="5.0" />
        </bean>
    </util:list>
  • 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)
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  • 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="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-3.0.xsd
        	http://www.springframework.org/schema/context
        	http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

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

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="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-3.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

	<context:component-scan base-package="org.lds.training" />
    </beans>
* 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
    <dependency>  
        <groupId>javax.inject</groupId>  
        <artifactId>javax.inject</artifactId>  
        <version>1</version>  
    </dependency>
  • 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 {
        @Inject
        @Named("prototypeRabbit")
        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 15:51.

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