jee、spring、spring mvc、mybatis 学习(五)
来源:互联网 发布:剑三成男捏脸数据 编辑:程序博客网 时间:2024/05/17 07:24
将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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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
0 0
- jee、spring、spring mvc、mybatis 学习(五)
- jee、spring、spring mvc、mybatis 学习(一)
- jee、spring、spring mvc、mybatis 学习(二)
- jee、spring、spring mvc、mybatis 学习(三)
- jee、spring、spring mvc、mybatis 学习(四)
- jee、spring、spring mvc、mybatis 学习(六)
- jee、spring、spring mvc、mybatis 学习(七)
- jee、spring、spring mvc、mybatis 学习(八)
- jee、spring、spring mvc、mybatis 学习(九)
- jee、spring、spring mvc、mybatis 学习(十)
- mybatis学习(五)--spring整合mybatis
- 学习spring mvc 五
- Spring Boot学习(MVC-MyBatis)
- SSM框架:Spring、Spring MVC、MyBatis学习
- Spring + Spring MVC + Mybatis 框架整合学习
- Spring mvc 五 基础学习
- 一点一滴学习Spring(五)之Spring MVC
- spring+mybatis+spring-mvc
- 1114. Family Property (25)-PAT甲级真题(并查集)
- easyquotation的配置与使用详细教程
- HDU 3007 Realtime Status 最小圆覆盖
- hdu4501 三维01背包,三个条件
- Java基础知识之变量和运算符号
- jee、spring、spring mvc、mybatis 学习(五)
- 最大正方形
- Java基础知识之方法
- HttpClient(GET、POST)请求网络数据
- JS作用域的问题
- Codeforce Gym 100819L : 2015-2016 ACM-ICPC Pacific Northwest Regional Contest - L 这是一道阅读理解(微笑)
- 开始标记
- QuantLib Installation in VC++ 2010
- 2016.8.12GDOI组模拟赛总结