applicationContext.xml簡單筆記
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <!-- <bean id="exceptionHandler" class="net.crm.base.exception.MyExceptionHandler"/> <mvc:annotation-driven /> <aop:aspectj-autoproxy /> <context:component-scan base-package="net.crm.*" ></context:component-scan>--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="dataSources" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="url" value="${jdbcUrl}" /> <property name="username" value="${user}" /> <property name="password" value="${password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="100" /> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,並且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat" /> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="initialPoolSize" value="10" /> <property name="maxIdleTime" value="100" /> <property name="maxPoolSize" value="200" /> <property name="minPoolSize" value="10" /> </bean> <!-- 配置Jdbc模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- <aop:config> <aop:pointcut id="servicePointcut" expression="execution(* *..service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/> </aop:config> <bean id="userAdvice" class="com.niuren.advice.UserAdvice"></bean> <aop:config> <aop:aspect id="userAop" ref="userAdvice"> <aop:pointcut id="target" expression="execution(* com.niuren.service.*.*(..))"/> <aop:before method="doBefore" pointcut-ref="target"/> </aop:aspect> </aop:config> --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:net/crm/**/mapper/*.xml" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>ApplicationResources</value> </list> </property> <property name="useCodeAsDefaultMessage" value="true" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="org.springframework.stereotype.Repository" /> <property name="basePackage" value="net.crm" /> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- 事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置那些類的方法進行事務管理 --> <aop:config> <aop:pointcut id="executeService" expression="execution(* net.crm.*.service..*(..))"/> <aop:advisor pointcut-ref="executeService" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="load*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="read*" read-only="true"/> <tx:method name="sync*"/> <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> <!-- 事務注解支持 <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 掃描注解文件 --> <mvc:annotation-driven /> <context:component-scan base-package="net.crm.*" /> <aop:aspectj-autoproxy /> </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:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!-- 自動掃描的包名,如果有多個包,請使用逗號隔開 --> <context:component-scan base-package="com.jieyou.*" /> <!-- 默認的注解映射的支持 --> <mvc:annotation-driven /> <!-- 啟動對aspectj的支持 --> <aop:aspectj-autoproxy/> <!-- 自動搜索指定包及其子包下的所有Bean類 --> <context:component-scan base-package="com.jieyou.*" /> <!-- 讀取屬性文件信息,將這些信息設置成Spring配置文件的數據 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 定義數據源Bean,使用C3P0數據源實現 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="initialPoolSize" value="10" /> <property name="maxIdleTime" value="100" /> <property name="maxPoolSize" value="200" /> <property name="minPoolSize" value="10" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--dataSource屬性指定要用到的連接池 --> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="org.springframework.stereotype.Repository" /> <property name="basePackage" value="com.jieyou" /> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- 配置JDBC數據源的局部事務管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事務增強處理Bean,指定事務管理器 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 所有已get開頭的方法是只讀的 --> <!-- <tx:method name="get*" read-only="true" propagation="REQUIRED" /> --> <!-- 其他方法使用默認的事務管理器 --> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- AOP配置元素 <aop:config> <aop:pointcut expression="execution(* com.jieyou.login_register.service.impl.*.*(..))" id="txPoint" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" /> </aop:config> --> </beans>