LDSTechForumProjects

Intermediate Spring

In this LDS Java Stack training we will build on what we learned in the Introduction to Spring and Introduction to Spring (part2) trainings. We will be discussing some more advanced concepts such as providers.

Prerequisites

Outline

Advanced Injection

  • Providers
  • Spring EL
  • Additional Injection Annotations

Web Context Listener

  • Configuring ContextLoaderListener
  • Accessing ApplicationContext in servlets
  • Utilizing inject in an HttpRequestHandler

Spring Testing Integration

  • Testability
  • Spring testing support


Slide Deck

Media:IntermediateSpring.pptx‎

Advanced Injection Presentation

Get Adobe Flash player

Lab 1 Advanced Injection

Use a Provider to defer instantiation of a "costly" operation

  • Add an init method to your rabbit bean with @PostConstruct
    @PostConstruct
    public void init() {
    	System.out.println(MessageFormat.format("Rabbit with name {0} being initialized.", name));
    }
* Add getPrizeRabbit method to your farm bean
    public Rabbit getPrizeRabbit() {
	return prizeRabbit;
    }
  • Demonstrate that it is called just by loading the farm bean (since the prototype rabbit is injected into the Farm bean)
  • Change the prizeRabbit reference in the farm bean to be a provider
    • Change all references to prizeRabbit to prizeRabbit.get();
  • Show that it does not call init until you reference the object with get()
    • Add the following into your SpringTester
        farm.getPrizeRabbit().printMealCount();
        farm.getPrizeRabbit().printMealCount();

Lab 1 Solution

Get Adobe Flash player

Web Context Listener Presentation

Get Adobe Flash player

Lab 2 Web Context Listener

Configure Web Context Listener and utilize injection in a request handler

  • Download the base project for the lab here - Media:spring-web-training.zip‎
    • Unzip the file into your workspace
    • In the LDSTech IDE go to File -> Import... -> Maven (expand) -> Existing Maven Projects -> Next
    • Browse to the unzipped project and press Finish
  • Configure the listener by putting the following in your web.xml
     <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:META-INF/spring/applicationContext.xml</param-value>
     </context-param>
  • Annotate the TrainingRequestHandler with @Component("trainingHandler")
  • Inject the SomeBean into the request handler (then printSomething() should be called on it in the handleRequest method)
  • Add the project to a regular Tomcat 6 server, (not an LDSTech Server) and start it
  • Hit http://localhost: 8080/spring-web-training/trainingHandler and verify that the print was executed

Lab 2 Solution

Get Adobe Flash player

Spring Testing Integration

Get Adobe Flash player

Lab 3 Spring Testing Integration

Write a test for the farm class using Spring’s test context

  • Add the following dependencies to your pom.xml
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.0.5.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>5.14.10</version>
      <scope>test</scope>
    </dependency>
  • Change the calculatePelletWeight in the Farm class to return the weight instead of just printing it out
  • Add the following test in src/test/java and package org.lds.training which you will have to create
    @ContextConfiguration(locations={"classpath*:*beans.xml","classpath*:*beans-test.xml"})
    public class FarmIT extends AbstractTestNGSpringContextTests {

	@Inject
	private Farm farm;
	
	@Test
	public void testCalculatePellets() {
		Assert.assertEquals(farm.calculatePelletWeight(), 0.1775, 0.005, "Pellet weight");
	}
    }
  • Import the testng Assert (not the JUnit one)
  • Add a resources folder to your src/test folder making it src/test/resources
  • Add a file named beans-test.xml with the following
  • Right click on the project and select Maven -> Update Project Configuration so these changes are picked up
  • Override the rabbitList by specifying a bean in the test application context with the same name
     <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"
	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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
	http://www.springframework.org/schema/context
        	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<util:list id="rabbitList">
		<bean class="org.lds.training.Rabbit">
			<constructor-arg name="weight" value="10" />
		</bean>
		<bean class="org.lds.training.Rabbit">
			<constructor-arg name="weight" value="10" />
		</bean>
		<bean class="org.lds.training.Rabbit">
			<constructor-arg name="weight" value="10" />
		</bean>
	</util:list>
   </beans>
  • Modify the farm test to verify that the rabbitList definition in the test file is overriding the application bean definition
	@Test
	public void testCalculatePellets() {
		Assert.assertEquals(farm.calculatePelletWeight(), 0.3, 0.005, "Pellet weight");
	}
  • Right click on the FarmIT class and select Run As -> TestNG Test

Lab 3 Solution

Get Adobe Flash player
This page was last modified on 1 November 2011, at 08:16.

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