Skip to content

Chapter 2 Lab

David Zemon edited this page Feb 6, 2017 · 5 revisions

Integration with Spring XML

This lab is intended to give you practice configuring your application using Spring. In the previous demo, you were shown how to extract a variable into a properties file and create a bean. Use this information to configure the remaining beans within the Main class.

Instructions

  1. Configure PokemonDao and PokemonLocationDao beans
  • Inside spring-context.xml, create beans for your PokemonDoa and PokemonLocationDoa.

  • Inside of main(), use context.getBean(...) to get the beans for those classes

  • Verify that you can still run the main() method

  • Configure the PokemonFactory bean

    • Notice that this one requires a constructor argument. Create the bean inside the configuration file. You need to add an ID to your RandomNumberGenerator bean so that you can reference it in your constructor argument.
    • Run the main() method again. If it can start up, we're good.
  • Configure the GameBoard bean

    • Notice that this bean has more constructor arguments. Add an ID to the beans which you need to reference and create the GameBoard bean.
    • Add the context.getBean(...) invocation to main() and verify that you can still run the application.
    • Notice you now have unused variables in your main() method. Go ahead and remove them.
  • Configure the Scanner bean.

    • The scanner bean requires a constant value: System.in. You can use the following snippet for creating beans from constants.
    <bean class="java.util.Scanner">
        <constructor-arg>
            <util:constant static-field="java.lang.System.in"/>
        </constructor-arg>
    </bean>
    • Note that your IDE may complain about util not being valid. Add this at the top of your 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-4.3.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-4.3.xsd">
    • Update the main() method to acquire a Scanner instance from the Spring container, just like the previous beans. You can now remove your declaration of the input variable since that is no longer used.
    • Verify you can still run main().
  • Configure the PrintStream bean

    <util:constant id="printStream" static-field="java.lang.System.out"/>
  • Configure the HumanInterface bean

    • Create your bean in the Spring context file, and add IDs to any beans we need to reference.
    • Clean up the main() method. Within the try block, you only need to get the HumanInterface bean and call the run method on it. Verify that you can still run the main() method.
    • Your final result should look like this
    public static void main(final String... args) {
      try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml")) {
        context.getBean(HumanInterface.class).run();
      }
    }

Clone this wiki locally