web.xml配置:
父容器:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringParent.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
子容器:
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:MySpring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
过滤器:跨域请求处理 需要在子容器中注册corsFilter对象
<filter>
<filter-name>myCorsFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>corsFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myCorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
子容器配置:
<!--开启包扫描,扫描controller层所有文件-->
<context:component-scan base-package="com.zhang">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--开启mvc注解识别-->
<mvc:annotation-driven/>
<!--对静态文件处理,解决覆盖servlet Default 静态文件处理-->
<mvc:default-servlet-handler/>
<!--跨域配置 :请求地址 请求方法 请求来源 请求头 请求是否允许携带cookies-->
<!--配置可以在过滤器中进行-->
<!--<mvc:cors>-->
<!--<mvc:mapping path="/**" allowed-methods="*" allowed-origins="http://192.168.13.231:8088" allowed-headers="*" allow-credentials="true"/>-->
<!--</mvc:cors>-->
</beans>
父容器配置:
<!--开启包扫描,扫描除Controller层以外所有文件-->
<context:component-scan base-package="com.zhang">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--引入配置Spring文件-->
<import resource="SpringDaoConfig.xml"/>
配置容器:
<!--引入properties文件-->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="minIdle" value="1" />
<property name="maxActive" value="50" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
</bean>
<!--sqlSessionFactory创建-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="typeAliases" value="com.zhang"/>
<property name="mapperLocations" value="classpath:mapper/*/*.xml"/>
<!--配置信息-->
<property name="configLocation" value="classpath:MybatisConfig.xml"/>
<!--插件-->
<!--<property name="plugins" value=""/>-->
</bean>
<!--dao层创建动态代理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.zhang.*.dao"/>
</bean>
<!--通过aop对目标service方法进行事物管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!--识别transation注解-->
<tx:annotation-driven/>
<!--<!–xml配置tansation管理方法–>-->
<!--<tx:advice id="txTransaction">-->
<!--<tx:attributes>-->
<!--<tx:method name="Select*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>-->
<!--</tx:attributes>-->
<!--</tx:advice>-->
<!---->
<!--<aop:config>-->
<!--<aop:pointcut id="myPointcut" expression="execution(* com.zhang.sys.*.service.*.*(..))"/>-->
<!--<aop:advisor advice-ref="txTransaction" pointcut-ref="myPointcut"/>-->
<!--</aop:config>-->
<!--跨域配置:基于过滤器设置 建议项目使用-->
<bean id="corsFilter" class="org.springframework.web.filter.CorsFilter">
<constructor-arg name="configSource">
<bean class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
<property name="corsConfigurations">
<map>
<entry key="/**">
<bean class="org.springframework.web.cors.CorsConfiguration">
<property name="allowCredentials" value="true"/>
<property name="allowedMethods">
<list>
<value>GET</value>
<value>POST</value>
<value>HEAD</value>
<value>PUT</value>
<value>DELETE</value>
<value>OPTIONS</value>
</list>
</property>
<property name="allowedHeaders" value="*"/>
<property name="allowedOrigins" value="http://localhost:8088"/>
</bean>
</entry>
</map>
</property>
</bean>
</constructor-arg>
</bean>
原文:https://www.cnblogs.com/zhang23/p/14703607.html