Spring配置文件基本作用

    Spring进行mvc开发中主要有三个配置文件web.xml、root-context.xml(名字可以自己更改)、servlet-context.xml(名字可以自己更改)

1.web.xml作用

主要定义应用的上下文applicationContext、spring使用的核心配置文件地址(也即root-context.xml)、spring对http请求路由使用的dispatcher、相应的控制器配置文件地址(也即servlet-context.xml)、匹配怎样的url地址路由到控制器中,一个web.xml实例:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- spring使用的核心配置文件地址 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    
    <!-- 应用的上下文applicationContext -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 相应的控制器配置文件地址 -->
    <servlet>
        <servlet-name>baobaotao</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--匹配怎样的url地址路由到控制器中-->
    <servlet-mapping>
        <servlet-name>baobaotao</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

2.spring使用的核心配置文件(root-context.xml)作用

主要配置spring在启动过程中需要加载的数据库访问层、服务层对应的bean,以及一些spring启动时需要加载的bean、数据源的配置等等,一个实例:

<?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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!--spring在启动过程中需要加载的数据库访问层、服务层对应的bean-->

 <context:component-scan base-package="com.baobaotao.dao" />
 <context:component-scan base-package="com.baobaotao.service" />

<!--数据源的配置-->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
         destroy-method="close"
         p:driverClassName="com.mysql.jdbc.Driver"
         p:url="jdbc:mysql://localhost:3306/sampledb"
         p:username="root"
         p:password=""/>
     <bean id="sqlSessionFactory"
         class="org.mybatis.spring.SqlSessionFactoryBean"
         p:dataSource-ref="dataSource"
         p:configLocation="classpath:../mybatisconfig/mybatisConfig.xml"/>
     <bean class="org.mybatis.spring.SqlSessionTemplate">
         <constructor-arg ref="sqlSessionFactory"/>
     </bean>

</beans>

3.spring使用的控制器配置文件(servlet-context.xml)作用

主要配置视图解析器和视图文件位置以及格式、控制器对应的包、静态资源目录位置、对请求体和响应体的解析器等等,一个实例如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.1.xsd">
    
    <!-- 开启@Controller可以被spring作为控制器 -->
    <annotation-driven />

    <!-- 将http中访问的静态文件映射到相应目录 -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- 配置视图解析器和视图文件位置以及格式-->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp">
    </beans:bean>
    <!-- 控制器对应的包 -->
    <context:component-scan base-package="com.baobaotao.web" />

<!--请求体和响应体的解析器-->

<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
         p:messageConverters-ref="messageConverters"/>
    <util:list id="messageConverters">
        <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
    </util:list>
</beans:beans>

 

转载于:https://my.oschina.net/seektechnology/blog/844424

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值