Skip to content

Chapter 2 Demo

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

Create the Spring Container

To create the Spring container, we'll need to add Spring as a dependency in our project and create the XML configuration file. Remember that this extra effort now will be repaid later as our project grows larger and more complex.

Part 1 - Create the Spring container

  1. Start by adding Spring as a compile-time dependency in the project. The group ID is org.springframework and we're looking for two artifacts: spring-core and spring-context. The version has been defined already as a Maven property, which can be referenced with ${spring.version}. For libraries and frameworks that have multiple artifacts using the same version, a Maven property like this can be a useful way to keep all artifact versions synchronized.
  • Next, we're ready to add the Spring configuration file. It's good to have a template to start out with, because the XML header is quite large. Drop the following contents into src/main/resources/spring-context.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
        <!-- Your beans here -->
    </beans>
  • Start by create a simple bean for the RandomNumberGenerator class. The syntax is simply <bean class="..."/>. At this point, the Spring configuration file is complete enough for a smoke test.

  • Creating a Spring container and providing our configuration file is as straightforward as instantiating an instance of ClassPathXmlApplicationContext and providing classpath:spring-context.xml to its constructor.

  • With the context instantiated and the configuration file complete, you're ready to replace the instantiation of RandomNumberGenerator in Main.java with a call to the contexts's .getBean(...) method. There are a few overloaded options available for that method - I recommend the one that takes a single parameter of type Class for this instance.

Part 2 - Reading from Properties Files

Properties files allow us to store constants in a configurable fashion as well as semi-constants (values that are constant for the lifetime of the JVM, but vary based on the environment).

  1. Create a file src/main/resources/common.properties. The syntax for these files is simple: name=value. To create a property named default-map-size with value 3, the file would like like this:

    default-map-size=3
  • For Spring to be able to read from properties files, we need to provide it with an instance of the PropertyPlaceholderConfigurer class. The location of our properties file(s) will be provided as dependencies of this PropertyPlaceholderConfigurer bean.

  • Property values are typically referenced only from Spring configuration files, but this step is a temporary necessity so that we can access the property from Main.java. Create a bean of type Integer with a single constructor argument. The constructor argument is how we will provide the property value:

    <bean id="defaultMapSize" class="java.lang.Integer">
        <constructor-arg value="${default-map-size}"/>
    </bean>
  • Finally, our property can be accessed by Java with an invocation of .getBean("defaultMapSize", Integer.class) on Spring context.

Clone this wiki locally