spring配置文件各个属性详解
一、引用外部属性文件
<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>
spring配置文件各个属性详解的更多相关文章
- Log4J 配置文件全属性详解
第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 1 log4j.rootCategory=INFO, stdou ...
- spring bean parent属性详解
必要条件:1.子bean必须与父bean保持兼容,也就是说子bean中必须有父bean定义的所有属性. 2.父bean必须是抽象bean或者定义lazy-init=true也就是不让bean工厂实例化 ...
- Spring中的@Transactional(rollbackFor = Exception.class)属性详解
序言 今天我在写代码的时候,看到了.一个注解@Transactional(rollbackFor = Exception.class),今天就和大家分享一下,这个注解的用法: 异常 如下图所示,我们都 ...
- coding++:Spring中的@Transactional(rollbackFor = Exception.class)属性详解
异常: 如下图所示,我们都知道 Exception 分为 运行时异常 RuntimeException 和 非运行时异常. error 是一定会回滚的. 如果不对运行时异常进行处理,那么出现运行时异常 ...
- SpringBoot配置文件 application.properties详解
SpringBoot配置文件 application.properties详解 本文转载:https://www.cnblogs.com/louby/p/8565027.html 阅读过程中若发现 ...
- Mybatis系列全解(四):全网最全!Mybatis配置文件XML全貌详解
封面:洛小汐 作者:潘潘 做大事和做小事的难度是一样的.两者都会消耗你的时间和精力,所以如果决心做事,就要做大事,要确保你的梦想值得追求,未来的收获可以配得上你的努力. 前言 上一篇文章 <My ...
- Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计
在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...
- Spring框架系列(8) - Spring IOC实现原理详解之Bean实例化(生命周期,循环依赖等)
上文,我们看了IOC设计要点和设计结构:以及Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的:容器中存放的是Bean的定义即Be ...
- Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现
我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...
随机推荐
- windows 标准错误重定向
最近在windows上运行tensorflow的时候,出现很多stderr 的信息,干扰了正常的输出:所以我们需要使用操作把这些输出屏蔽: 参考链接:https://support.microsoft ...
- SpringBoot+Shiro+mybatis整合实战
SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...
- jeecms系统使用介绍——jeecms中的内容、栏目、模型之间的关系
转载:https://blog.csdn.net/dongdong9223/article/details/76578120 jeecms是一款很不错的cms产品,之前在文章<基于Java的门户 ...
- day36 05-Hibernate检索方式:离线条件查询
图二 离线条件查询 Struts 2是web层的框架.session得在dao层才有.有的时候这些数据是没在你的对象里面的.像是否上传简历.这个字段不在我的数据库里面.例如是否上传简历这个条件不在我们 ...
- 关于memset的使用
有些oj上的G++支持 cstdio的memset,有些则支持stdio.h中的memset(划掉) 这两个头文件关系比较复杂, 具体我也说不清...按照c++文档中的说法,stdio已经deprec ...
- android rsa 示例
1.参考资料 1.1 android的Cipher官方文档 https://developer.android.com/reference/javax/crypto/Cipher 其中 构造Ciphe ...
- Python运用于数据分析的简单教程
Python运用于数据分析的简单教程 这篇文章主要介绍了Python运用于数据分析的简单教程,主要介绍了如何运用Python来进行数据导入.变化.统计和假设检验等基本的数据分析,需要的朋友可以参考下 ...
- Python判断文件和文件夹是否存在的方法
Python判断文件和文件夹是否存在的方法 这篇文章主要介绍了Python判断文件和文件夹是否存在的方法,本文还讲解了判断是否为文件或者目录的方法.os.path.lexist的作用.FTP中判断文件 ...
- unity如何查找某个脚本挂在了哪些物体上
在开发中往往会遇到一个问题:不知道整个场景中究竟有哪些物体挂载了某一个脚本.如果挨个查找太麻烦了,下面有一种方法可以快速找到解决这个问题. 在unity的Window里有一项Editor tests ...
- 洛谷 P2955 [USACO09OCT]奇数偶数Even? Odd?【字符串/易错】
题目描述 Bessie's cruel second grade teacher has assigned a list of N (1 <= N <= 100) positive int ...