一、引用外部屬性文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
我們定義了一個PropertyPlaceholderConfigurer類的實例,並將其位置屬性設置為我們的屬性文件。該類被實現為Bean工廠的后處理器,並將使用定義在文件中的屬性來代替所有的占位符(${...}value)。
注意:
而在spring2.5的版本中提供了一種更簡便的方式,如:
- <context:property-placeholder location="classpath:config/jdbc.properties"/>
這樣以后要使用屬性文件中的資源時,可以使用${屬性名}來獲得。
二、常用數據源的配置
第一種是:DBCP數據源,(需要加入2個jar文件,在spring中的lib下jakarta-commons/commons-dbcp.jar和commons-pools.jar)主要配置如下:
<!-- Mysql版 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="${jdbc.url}">
</property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
第二種是:c3p0數據源,跟第一種一個類型,需加入c3p0.jar包。
第三種是:JNDI數據源,配置在高性能的應用服務器(如WebLogic、WebSphere等)
- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName" value="java:comp/env/jdbc/bbt"/>
- </bean>
從spring2.0開始提供jee命名空間,可以簡化配置如下:
- <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/bbt"/>
三、配置事務管理器
1、Spring JDBC 和 iBatis事務管理器的配置
<!-- 配置事務管理器 -->
<bean id="TransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
2、Hibernate3以上事務管理器的配置(先要集成hibernate,再配置事務管理器)
- <!-- 集成hibernate -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- <value>classpath:product.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- </props>
- </property>
- </bean>
- <!-- 配置Hibernate事務策略 -->
- <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
3、配置tx/aop聲明式事務
<!-- 聲明一個切面 -->
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
- <tx:method name="save*" propagation="REQUIRED"/>
- <tx:method name="update*" propagation="REQUIRED"/>
- <tx:method name="*" propagation="SUPPORTS" read-only="true" />
- </tx:attributes>
- </tx:advice>
別的例子,可以進行對比下:
<tx:advice id="userTxAdvice" transaction-manager="TransactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.RuntimeException" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="select*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 把切面注入到業務中 -->
- <aop:config>
- <aop:pointcut id="productServiceMethods" expression="execution(* com.wzc.student.business.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
- </aop:config>
對比:
<aop:config>
<aop:pointcut id="pc" expression="execution(public * com.haso.bscsserver.service.*.*(..))" /> <!--把事務控制在Service層-->
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config>
四、context:component-scan
<!-- 對包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->
<context:component-scan base-package="com.haso.bscsserver">
<!-- 允許定義過濾器將基包下的某些類納入或排除
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
</context:component-scan>
請參考http://blog.csdn.net/ydwuli06/article/details/6993219,具體的自己還深入研究過
五、aop注解支持
<!-- aop注解支持 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
六、緩存配置
<!-- 緩存配置 -->
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/ehcache.xml"/>
</bean>
<!-- A facade to the Ehcache cache class -->
<bean id="cacheProviderFacade"
class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
<property name="cacheManager" ref="cacheManager" />
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
ehcache.xml文件:
<!--
name:Cache的唯一標識
maxElementsInMemory:內存中最大緩存對象數
maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大
eternal:Element是否永久有效,一但設置了,timeout將不起作用
overflowToDisk:配置此屬性,當內存中Element數量達到maxElementsInMemory時,Ehcache將會Element寫到磁盤中
timeToIdleSeconds:設置Element在失效前的允許閑置時間。僅當element不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大
timeToLiveSeconds:設置Element在失效前允許存活時間。最大時間介於創建時間和失效時間之間。僅當element不是永久有效時使用,默認是0.,也就是element存活時間無窮大
diskPersistent:是否緩存虛擬機重啟期數據
diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒
diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用)
-->
<defaultCache overflowToDisk="true" eternal="true"/>
<diskStore path="C:/cache" />
<cache name="zzugxy" overflowToDisk="true" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="1000"
maxElementsOnDisk="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300"
diskSpoolBufferSizeMB="100" memoryStoreEvictionPolicy="LRU" />
</ehcache>
******************************************************************************************************************************************************************************
注解緩存的配置:
參考(出自http://blog.csdn.net/gaoligaoli/article/details/4282403):
關於spring實現ehcache有很多方法,很多都是利用aop來實現,我認為采用注解的方式更靈活,配置也更簡潔。下面就是我利用spring-modules-0.9實現的注解緩存。
配置文件如下:
- <?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:ehcache="http://www.springmodules.org/schema/ehcache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springmodules.org/schema/ehcache
- http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
- <ehcache:config configLocation="classpath:ehcache.xml"/>
- <ehcache:annotations>
- <ehcache:caching id="testCache" cacheName="testCache" />
- <ehcache:flushing id="testFlush" cacheNames="testCache"/>
- </ehcache:annotations>
- </beans>
這里一定要注意:
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
和
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
我就在這里花了很長時間,查了很多資料。網上的很多資料說這是spring-moduls的bug。很多朋友在這里總是報找到xsd文件。
在ehcache.xml中加入
- <cache name="testCache" maxElementsInMemory="20000"
- maxElementsOnDisk="1000" eternal="true" overflowToDisk="true"
- memoryStoreEvictionPolicy="LFU" />
***************************************************************************************************************************************************************************
七、
<!-- Spring、MyBatis的整合,需要在 Spring 應用上下文中定義至少兩樣東西:一個SqlSessionFactory和至少一個數據映射器類(UserMapper->iocContext.xml)。 -->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
轉自:http://blog.csdn.net/snn1410/article/details/7846582