jee、spring、spring mvc、mybatis 学习(五)
2016-08-13 16:40
579 查看
将spring、spring MVC的配置文件分开
转载请申明转载出处:http://blog.csdn.net/qq5132834/article/details/52200115
1、本节将spring的配置问题(自动注入)。之前在第三、四中已经提到自动注入,并且在配置文件【mvc-servlet.xml】中实现了自动注入的方式,内容如下:
这种方式确实可以实现自动注入,但是我多方查阅资料,建议不要采用这种混写在一起的方式,而是要将【spring mvc servlet】的配置和【spring的配置】分开来,建议将注入写入【spring.xml】文件中,这时只需从【mvc-servlet.xml】中剪切以下配置(将他复制到新建的spring.xml文件中):
2、在【src/sources】文件夹中新建一个【spring.xml】文件内容如下:
3、在【web.xml】文件中加载新建的【spring.xml】。此时,应该在web.xml文件中配置spring的监听。此时web.xml文件的内容如下:
观上可知,web.xml文件中新增加了两个配置信息,分别是:
【spring.xml】的加载信息:
4、访问路径:http://localhost:8080/ZZZ/LoginController/login.xhtml
5、源代码:http://download.csdn.net/detail/qq5132834/9602553
转载请申明转载出处: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
相关文章推荐
- 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 学习(七)
- Spring学习5_ Spring MVC与mybatis学习-----参数配置文件说明
- Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成
- SSM框架(Spring Spring MVC Mybatis)基础入门学习3——Mybits入门实例(实例免费下载!)
- SSM框架:Spring、Spring MVC、MyBatis学习
- Spring学习6_Spring MVC与mybatis学习-----执行路程详解
- Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器
- SpringBoot学习笔记之mybatis分页插件集成
- Spring MVC 学习笔记(一) 基于spring2.5的纯xml配置
- Spring+SpringMVC+mybatis+easyui整合实例(三)spring mvc整合及mybatis事务部分
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- 【MyBatis学习14】MyBatis和Spring整合