Datasheet
2. Creates an instance of OpMultiply and names the bean multiply
3. Creates an instance of OpAdd and names the bean add
4. Creates an instance of CalculateSpring and names the bean opsbean
5. Sets the reference of the ops property of the opsbean bean to the bean named multiply
6. Sets the reference of the writer property of the opsbean bean to the bean named screen
Each of these instructions is labeled in bold in the following reproduction of the beans.xml context
descriptor.
<?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-2.0.xsd”>
(1)<bean id=”screen” class=”com.wrox.begspring.ScreenWriter” />
(2)<bean id=”multiply” class=”com.wrox.begspring.OpMultiply” />
(3)<bean id=”add” class=”com.wrox.begspring.OpAdd” />
(4)<bean id=”opsbean” class=”com.wrox.begspring.CalculateSpring”>
(5)<property name=”ops” ref=”multiply” />
(6)<property name=”writer” ref=”screen”/>
</bean>
</beans>
It is very important for you to understand how Java classes are created and wired using a Spring context
descriptor. Take a careful look and make sure you see how these actions are carried out.
The net effect is that the
CalculateSpring logic will be wired with the OpMultiply operation and the
ScreenWriter writer. This is why you see the result of the multiplication on the screen when you run
CalculateSpring.
Adding a Logging Configuration File
In addition to the beans.xml, you also need to create a log4j.properties file. The Spring framework
uses Apache Commons Logging (about which information is available at
http://jakarta.apache.org/
commons/logging/
) to log container and application information. Commons logging can work with a
number of loggers, and is configured to work with Apache’s Log4j library (an open-source library; infor-
mation is available at
http://logging.apache.org/log4j/docs/index.html).
Log4j can read its configuration information from a properties file. In the properties file, you can config-
ure appenders that control where the logging information is written to; for example, to a log file versus
to the screen). You can also control the level of logging; for example, a log level of
INFO prints out a lot
more information than a log level of
FATAL, which only prints out fatal error messages. The following
log4j.properties file is used by the example(s) and only displays fatal messages to the console. (You
can find it in
src\springfirst\src\main\resources.) Maven automatically copies this file into
the correct location and constructs the classpath for the application (via the information provided in the
pom.xml file). Then Log4J uses the classpath to locate the configuration file.
13
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 13