1. 程式人生 > >【SSM】【3】spring容器配置

【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>