Spring In Action(Sping实战)

内容概述:

1. 基本概念

Dependency Injection
Aspect Oriented Programming

ApplicationContext.xml v.s. spring-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
Spring lets you define multiple contexts in a parent-child hierarchy.  

The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.

The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).

Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.

All Spring MVC controllers must go in the spring-servlet.xml context.

In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

2. 同时配置RootContext和多个DispatcherServletContext

目标是在一个springMVC应用中配置1个RootContext和3个DispatcherServlet:
2.1 首先编辑web.xml:配置Root Context

1
2
3
4
5
6
7
8
9
<!-- 指定根应用上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

配置第一个Servlet Context:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- Dispacther应用上下文, 如果这里不指定contextConfigLocation,那么默认从WEB-INF/myServlet-context.xml加载beans -->
<servlet>
<servlet-name>lakersServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/lakers-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>
<servlet-name>lakersServlet</servlet-name>
<url-pattern>/lakers</url-pattern>
</servlet-mapping>

配置第二个Servlet Context:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- Dispacther应用上下文, 如果这里不指定contextConfigLocation,那么默认从WEB-INF/myServlet-context.xml加载beans -->
<servlet>
<servlet-name>rocketsServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rockets-context.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>
<servlet-name>rocketsServlet</servlet-name>
<url-pattern>/rockets</url-pattern>
</servlet-mapping>

2.2 然后配置applicationContext.xml:

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans...">

<!--注意这里不要去设置component-scan-->
<!-- <context:component-scan base-package="edu.zju.springmvc" /> -->
<bean id="teacher" class="edu.zju.springmvc.config.Teacher">
<constructor-arg value="Confucious" />
<constructor-arg value="1" />
</bean>
</beans>

2.3 最后分别配置3个DispatcherServletContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<beans xmlns="http://www.springframework.org/schema/beans...>

<context:component-scan base-package="edu.zju.springmvc.lakers" />
<bean id="stu" class="edu.zju.springmvc.entity.Student">
<constructor-arg value="KobeBryant" />
<constructor-arg value="24" />
</bean>

<bean id="player" class="edu.zju.springmvc.entity.Player">
<constructor-arg value="Lonzo Ball" />
<constructor-arg value="2" />
</bean>


<!-- ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里就只能解析/WEB-INF/jsp/目录下的jsp文件 -->
<property name="prefix" value="/WEB-INF/lakers/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


3. SpringMvc配置使用slf4j

3.1 配置pom.xml文件

1
2
3
4
5
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha2</version>
</dependency>

3.2 在web.xml加入以下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- Log4j配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log/log4j.xml</param-value>
</context-param>
<!-- 加载log4j配置文件 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>rootLevel</param-name>
<param-value>INFO</param-value>
</context-param>

<context-param>
<param-name>loggingLevel</param-name>
<param-value>INFO</param-value>
</context-param>

3.3 添加/WEB-INF/log/log4j.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- [控制台STDOUT] -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="encoding" value="GBK" />
<param name="target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{2} - %m%n" />
</layout>
</appender>

<!-- [公共Appender] -->
<appender name="DEFAULT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${webapp.root}/logs/default.log" />
<param name="Append" value="true" />
<param name="encoding" value="GBK" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
</layout>
</appender>

<!-- [错误日志APPENDER] -->
<appender name="ERROR-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${webapp.root}/logs/error.log" />
<param name="Append" value="true" />
<param name="encoding" value="GBK" />
<param name="threshold" value="error" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
</layout>
</appender>

<!-- [组件日志APPENDER] -->
<appender name="COMPONENT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${webapp.root}/logs/component.log" />
<param name="Append" value="true" />
<param name="encoding" value="GBK" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
</layout>
</appender>

<!-- [组件日志] -->
<logger name="LOGISTICS-COMPONENT">
<level value="${loggingLevel}" />
<appender-ref ref="COMPONENT-APPENDER" />
<appender-ref ref="ERROR-APPENDER" />
</logger>

<!-- Root Logger -->
<root>
<level value="${rootLevel}"></level>
<appender-ref ref="DEFAULT-APPENDER" />
<appender-ref ref="ERROR-APPENDER" />
</root>

</log4j:configuration>

3.4 之后就可以在代码中直接使用了,Tomcat的webapps目录下面对应的应用中就可以看到生成的日志文件