-
Notifications
You must be signed in to change notification settings - Fork 5
Chapter 2 Lab
David Zemon edited this page Feb 6, 2017
·
5 revisions
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.
- Configure
PokemonDaoandPokemonLocationDaobeans
-
Inside
spring-context.xml, create beans for yourPokemonDoaandPokemonLocationDoa. -
Inside of
main(), usecontext.getBean(...)to get the beans for those classes -
Verify that you can still run the
main()method -
Configure the
PokemonFactorybean- Notice that this one requires a constructor argument. Create the bean inside the configuration file. You need to
add an ID to your
RandomNumberGeneratorbean so that you can reference it in your constructor argument. - Run the
main()method again. If it can start up, we're good.
- Notice that this one requires a constructor argument. Create the bean inside the configuration file. You need to
add an ID to your
-
Configure the
GameBoardbean- Notice that this bean has more constructor arguments. Add an ID to the beans which you need to reference and create
the
GameBoardbean. - Add the
context.getBean(...)invocation tomain()and verify that you can still run the application. - Notice you now have unused variables in your
main()method. Go ahead and remove them.
- Notice that this bean has more constructor arguments. Add an ID to the beans which you need to reference and create
the
-
Configure the
Scannerbean.- 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
utilnot 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 aScannerinstance 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().
- The scanner bean requires a constant value:
-
Configure the
PrintStreambean<util:constant id="printStream" static-field="java.lang.System.out"/>
-
Configure the
HumanInterfacebean- Create your bean in the Spring context file, and add IDs to any beans we need to reference.
- Clean up the
main()method. Within thetryblock, you only need to get theHumanInterfacebean and call the run method on it. Verify that you can still run themain()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(); } }