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.



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


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

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 -‎
    • 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
  • 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
  • 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 which you will have to create
    public class FarmIT extends AbstractTestNGSpringContextTests {

	private Farm farm;
	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="" xmlns:xsi=""

	<util:list id="rabbitList">
		<bean class="">
			<constructor-arg name="weight" value="10" />
		<bean class="">
			<constructor-arg name="weight" value="10" />
		<bean class="">
			<constructor-arg name="weight" value="10" />
  • Modify the farm test to verify that the rabbitList definition in the test file is overriding the application bean definition
	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.