-
Notifications
You must be signed in to change notification settings - Fork 5
Chapter 2 Demo
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.
- Start by adding Spring as a compile-time dependency in the project. The group ID is
org.springframeworkand we're looking for two artifacts:spring-coreandspring-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
RandomNumberGeneratorclass. 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
ClassPathXmlApplicationContextand providingclasspath:spring-context.xmlto its constructor. -
With the context instantiated and the configuration file complete, you're ready to replace the instantiation of
RandomNumberGeneratorinMain.javawith 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 typeClassfor this instance.
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).
-
Create a file
src/main/resources/common.properties. The syntax for these files is simple:name=value. To create a property nameddefault-map-sizewith value3, 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
PropertyPlaceholderConfigurerclass. The location of our properties file(s) will be provided as dependencies of thisPropertyPlaceholderConfigurerbean. -
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 typeIntegerwith 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.