一、引用外部属性文件

<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的版本中提供了一种更简便的方式,如:

  1. <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等)
  1. <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  2. <property name="jndiName" value="java:comp/env/jdbc/bbt"/>
  3. </bean>

从spring2.0开始提供jee命名空间,可以简化配置如下:

  1. <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,再配置事务管理器)
  1. <!-- 集成hibernate -->
  2. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  3. <property name="dataSource" ref="dataSource"/>
  4. <property name="mappingResources">
  5. <list>
  6. <value>classpath:product.hbm.xml</value>
  7. </list>
  8. </property>
  9. <property name="hibernateProperties">
  10. <props>
  11. <prop key="hibernate.dialect">
  12. </props>
  13. </property>
  14. </bean>
  15. <!-- 配置Hibernate事务策略 -->
  16. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  17. <property name="sessionFactory" ref="sessionFactory"></property>
  18. </bean>
3、配置tx/aop声明式事务

<!-- 声明一个切面 -->

  1. <tx:advice id="txAdvice" transaction-manager="txManager">
  2. <tx:attributes>
  3. <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
  4. <tx:method name="save*" propagation="REQUIRED"/>
  5. <tx:method name="update*" propagation="REQUIRED"/>
  6. <tx:method name="*" propagation="SUPPORTS" read-only="true" />
  7. </tx:attributes>
  8. </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>

<!-- 把切面注入到业务中 -->

  1. <aop:config>
  2. <aop:pointcut id="productServiceMethods" expression="execution(* com.wzc.student.business.*.*(..))" />
  3. <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
  4. </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实现的注解缓存。

配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  16. http://www.springmodules.org/schema/ehcache
  17. http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
  18. <ehcache:config configLocation="classpath:ehcache.xml"/>
  19. <ehcache:annotations>
  20. <ehcache:caching id="testCache" cacheName="testCache" />
  21. <ehcache:flushing id="testFlush" cacheNames="testCache"/>
  22. </ehcache:annotations>
  23. </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中加入

  1. <cache name="testCache" maxElementsInMemory="20000"
  2. maxElementsOnDisk="1000" eternal="true" overflowToDisk="true"
  3. 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>

最新文章

  1. delphi真随机数发生器
  2. 找回忘记的Ubuntu用户名和密码
  3. 如何判断TCP包是否发送成功
  4. Swift学习笔记一
  5. OAuth 的权限问题与信息隐忧
  6. /sbin/ifup: configuration for eth0 not found解决
  7. javascript每日一练(十四)——弹性运动
  8. 14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚
  9. 深入C#.NET数据类型
  10. react_app 项目开发
  11. 405 Method Not Allowed error with PUT or DELETE Request on IIS Server
  12. 为github公开项目单独设置用户名
  13. ThinkPHP框架 祖辈分的理解 【儿子 FenyeController】继承了【父亲 FuController】继承了【祖辈 Controller】的
  14. No mapping found for HTTP request with URI [/Portal/download] in DispatcherServlet with name &#39;springmvc&#39;
  15. java多线程14 :wait()和notify()/notifyAll()
  16. js 退后一步并刷新,window.history.back(-1);这个只能后退一步不能刷新,
  17. Windows10 Virtualization Technology虚拟化技术功能
  18. VMware 安装Ubuntu16.04时显示不全的解决方法
  19. python 列表的内容赋值
  20. Linux Tomcat 7.0 管理员登录时&quot;401 Unauthorized&quot; 问题解决方法

热门文章

  1. DLX
  2. Java review-basic2
  3. jframe 设置左上角和任务栏的图标
  4. JQMObile 优势
  5. dom元素分屏加载
  6. Django中form组件的is_valid校验机制
  7. vue里调用moment.js
  8. PHP--y2k38的解决方法已经时间格式的常用转换
  9. Hdu 1150
  10. 【JZOJ5060】【GDOI2017第二轮模拟day1】公路建设 线段树+最小生成树