【SSM】【3】spring容器配置

由web.xml可以看出来,在<context></context>中配置了上下文扫描

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/*.xml</param-value>
    </context-param>

现在就对上下文进行配置,你在<param-value></param-value>配置的路径就是你的上下文路径,本次配置的意思就是在spring文件夹下的所有.xml文件

  1. spring-dao.xml配置:spring整合mybatis,配置数据源,配置数据工厂,配置事物可以通用注解的形式启用
<?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: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/tx
	   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<!-- scan for mappers and let them beautowired -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.**.**.module.**.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
		<property name="mapperLocations" value="classpath*:com/**/**/module/**/entity/mapper/*Mapper.xml" />
	</bean>

	<bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
		<property name="statementExecutableSqlLogEnable" value="false" />
	</bean>

	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		  init-method="init" destroy-method="close">
		<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="${dbcp.initialSize}" />
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="minIdle" value="${dbcp.minIdle}" />

		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="${dbcp.maxWait}" />

		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="${dbcp.timeBetweenEvictionRunsMillis}" />

		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="${dbcp.minEvictableIdleTimeMillis}" />

		<property name="testWhileIdle" value="${dbcp.testWhileIdle}" />

		<!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
		<property name="testOnBorrow" value="${dbcp.testOnBorrow}" />
		<property name="testOnReturn" value="${dbcp.testOnReturn}" />

		<!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->

		<property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}" />

		<!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
		<property name="validationQuery" value="select 1  FROM DUAL" />
		<property name="filters" value="${dbcp.filters}" />
		<property name="proxyFilters">
			<list>
				<ref bean="logFilter" />
			</list>
		</property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>
  1. spring-main.xml:spring整合
    1. spring通过注解的形式加载propertites文件,
    2. 配置json转换
    3. spring配置全局扫描
<?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:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:task="http://www.springframework.org/schema/task"
       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-2.5.xsd
        http://www.springframework.org/schema/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd
">
    <bean id="myPropertiesBean"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:ecif_config.properties</value>
            </list>
        </property>
    </bean>
    <bean id="jacksonHttpMessageConverter"
          class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>

    <context:component-scan
            base-package=" com.**.**.utils,com.**.**.module.**,com.edhic.ecif.listenter,com.edhic.ecif.filter,com.edhic.ecif.service"/>

</beans>

猜你喜欢

转载自blog.csdn.net/liangayang/article/details/83089115