将spring、spring MVC的配置文件分开
转载请申明转载出处:http://blog.csdn.net/qq5132834/article/details/52200115
1、本节将spring的配置问题(自动注入)。之前在第三、四中已经提到自动注入,并且在配置文件【mvc-servlet.xml】中实现了自动注入的方式,内容如下:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.zuk.services" />
<!-- 扫描com.zuk.services这个包里所以的service类,【务必在类上标注:@Component】,此时可以采用@Autowired实现自动注入这个包里面全部的对象 -->
<context:component-scan base-package="com.zuk.controllers" />
<!-- 扫描com.zuk.controllers这个包里的所以controller类,【务必在类上标注:@Controller】 -->
这种方式确实可以实现自动注入,但是我多方查阅资料,建议不要采用这种混写在一起的方式,而是要将【spring mvc servlet】的配置和【spring的配置】分开来,建议将注入写入【spring.xml】文件中,这时只需从【mvc-servlet.xml】中剪切以下配置(将他复制到新建的spring.xml文件中):
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.zuk.services" />
<!-- 扫描com.zuk.services这个包里所以的service类,【务必在类上标注:@Component】,此时可以采用@Autowired实现自动注入这个包里面全部的对象 -->
2、在【src/sources】文件夹中新建一个【spring.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- service包(自动注入) -->
<context:component-scan base-package="com.zuk.services" />
<!-- 扫描com.zuk.services这个包里所以的service类,【务必在类上标注:@Component】,此时可以采用@Autowired实现自动注入这个包里面全部的对象 -->
</beans>
3、在【web.xml】文件中加载新建的【spring.xml】。此时,应该在web.xml文件中配置spring的监听。此时web.xml文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ZZZ</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:sources/spring.xml</param-value>
</context-param>
<filter>
<description>字符集过滤器</description>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring的监听器 -->
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载spring mvc servlet -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:sources/mvc-servlet.xml</param-value>
<!-- 指定mvc-servlet.xml文件路径,在类src/sources路径下面 -->
</init-param>
<load-on-startup>1</load-on-startup>
<!-- load-on-startup:表示启动容器时初始化该Servlet -->
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.xhtml</url-pattern>
<!-- 表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的 -->
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.json</url-pattern>
<!-- “*.json”表示拦截所有以json为扩展名的请求。 -->
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.xml</url-pattern>
<!-- “*.xml”表示拦截所有以xml为扩展名的请求。 -->
</servlet-mapping>
</web-app>
观上可知,web.xml文件中新增加了两个配置信息,分别是:
【spring.xml】的加载信息:
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:sources/spring.xml</param-value>
</context-param>
【spring的监听】信息:
<!-- spring的监听器 -->
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4、访问路径:http://localhost:8080/ZZZ/LoginController/login.xhtml
5、源代码:http://download.csdn.net/detail/qq5132834/9602553