spring基于xml文件父子容器配置
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--解决中文乱码-->
<filter>
<filter-name>charset</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charset</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--用监听器配置root容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-root.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置MVC框架-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
父容器
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描service访问-->
<context:component-scan base-package="com.example.service"></context:component-scan>
<!--加载jdbc配置文件-->
<context:property-placeholder location="classpath:properties/jdbc-info.properties"></context:property-placeholder>
<!--放置数据源连接池对象-->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--将配置文件参数传入连接池-->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
</bean>
<!--将Spring支持的jdbcTemplate放入容器中(使用myBatis可以不配)-->
<bean name="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource" ></constructor-arg>
</bean>
<!--事务管理器-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务管理驱动-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--整合myBatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置myBatis的数据源-->
<property name="dataSource" ref="dataSource" />
<!--configLocation用于指定全局配置文件 (一般这里里面的配置是通过直接在这个bean上面来注入) -->
<property name="configLocation" value="classpath:mybatis-conf.xml"/>
<!--mapper映射配置文件的位置: 可以利用Spring的资源表示法(通配符)-->
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
<!--mvc扫描mapper层,自动注册到spring容器中-->
<!--<mybatis:scan base-package="com.example.mapperspers" annotation="org.apache.ibatis.annotations.Mapper"></mybatis:scan>-->
<!--<mvc:scan base-package="com.example.mapperspers" annotation="org.apache.ibatis.annotations.Mapper"></mvc:scan>-->
<mybatis:scan base-package="com.example.mapper" />
</beans>
子容器
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--将controller扫描到spring容器中-->
<context:component-scan base-package="com.example.controller"></context:component-scan>
<!--开启mvc驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--设置视图解析-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!---->
<!--静态资源访问方式一处理完后将控制权交给TomCat-->
<mvc:default-servlet-handler default-servlet-name="default" ></mvc:default-servlet-handler>
<!--使用springMvc自带的静态资源管理器-->
<!--<mvc:resources mapping="/**" location="css"></mvc:resources>-->
<!--<mvc:resources mapping="/css/**" location="classpath:css/"></mvc:resources>-->
<!--拦截器设置-->
<!--<mvc:interceptors>-->
<!--<bean class="com.example.handler.MyHandler1"></bean>-->
<!--<mvc:interceptor>-->
<!--<mvc:mapping path="/**"/>-->
<!--<mvc:exclude-mapping path="/admin/"></mvc:exclude-mapping>-->
<!--<bean class="com.example.handler.MyHandler1"></bean>-->
<!--</mvc:interceptor>-->
<!--</mvc:interceptors>-->
</beans>
转载请注明原文地址:https://blackberry.8miu.com/read-38035.html