ssm框架中,web项目中applicationContext.xml及相关配置文件解析

一、概述

applicationContext.xml,即Spring上下文配置文件,用于完成Spring和MyBatis的整合。主要配置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: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-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<context:component-scan base-package="com.atguigu">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!--spring的配置文件,这里主要配置与事务逻辑有关的 -->
	<!--数据源,整合mybatis,事务控制,、、、、、 -->

	<context:property-placeholder location="classpath:dbconfig.properties" />
	<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!--配置和mybatis的整合 -->
        <!--配置会话工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<property name="dataSource" ref="pooledDataSource" />
		<property name="mapperLocations" value="classpath:mapper/*.xml" />
	</bean>
	<!--配置扫描器,将mybatis接口的实现加入到IOC容器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.atguigu.crud.dao" />
	</bean>
	
	<!--配置一个可批量处理的sqlSession  -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
		<constructor-arg name="executorType" value="BATCH"></constructor-arg>
	</bean>

	<!--事务控制的配置 -->
	<!--事务管理器  -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!--控制住数据源  -->
		<property name="dataSource" ref="pooledDataSource" />
	</bean>
	<!--开启基于注解的事务,使用xml配置形式的配置(比较重要的都是使用配置式)  -->
	
	<aop:config>
		<!--切入点表达式  -->
		<aop:pointcut expression="execution(* com.atguigu.crud.service..*(..))" id="txPoint"/>
		<!--配置事务增强,切入点+切入规则  -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
	</aop:config>
	<!--配置事务增强,事务切入的规则  -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<!--以find开头的所有方法都是事务方法  -->
			<tx:method name="find*" read-only="true" />
			<tx:method name="to*" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
		</tx:attributes>
	</tx:advice>
</beans>

二、各部分解析

1.配置扫描器,将除Controller层以外的类装配到Spring容器

<context:component-scan base-package="com.atguigu">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

2.配置数据源

方法(1)

<context:property-placeholder location="classpath:dbconfig.properties" />
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	<property name="driverClass" value="${jdbc.driverClass}"></property>
	<property name="user" value="${jdbc.user}"></property>
	<property name="password" value="${jdbc.password}"></property>
</bean>

applicationContext.xml文件同级目录(类路径)下创建dbconfig.properties文件

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=******
jdbc.password=*******

方法(2)

<util:properties id="jdbc" location="classpath:jdbc.properties"/>
<bean id="dataSource" 
    class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
	<property name="url" value="#{jdbc.url}"/>
	<property name="driverClassName" value="#{jdbc.driver}"/>
	<property name="username" value="#{jdbc.user}"/>
	<property name="password" value="#{jdbc.password}"/>
</bean>

applicationContext.xml文件同级目录(类路径)下创建jdbc.properties文件

url=jdbc:mysql://localhost:3306/cscbms?characterEncoding=utf-8&allowMultiQueries=true
driver=com.mysql.jdbc.Driver
user=******
password=******

 3.整合MyBatis

<!--配置和mybatis的整合 -->
<!--配置会话工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="configLocation" value="classpath:mybatis-config.xml" />
	<property name="dataSource" ref="pooledDataSource" />
	<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!--配置扫描器,将mybatis接口的实现加入到IOC容器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.atguigu.crud.dao" />
</bean>
	
<!--配置一个可批量处理的sqlSession  -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
	<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>

配置sqlSessionFactory时需要引用mybatis-config.xml(用于配置mybatis相关参数),mybatis-config.xml编写方法见http://www.mybatis.org/mybatis-3/zh/getting-started.html(mybatis中文参考文档)。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true "></setting>
	</settings>
	<typeAliases>
		<package name="com.atguigu.crud.bean" />
	</typeAliases>
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor">
			<!--分页参数合理化  -->
			<property name="reasonable" value="true"/>
		</plugin>
	</plugins>
</configuration>

猜你喜欢

转载自blog.csdn.net/qq_38861828/article/details/85109022