您的位置:首页 > 编程语言 > Java开发

Spring+hibernate整合常用的配置文件

2010-10-23 14:01 471 查看
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
<context:annotation-config /> <!-- 打开注解配置-->
<context:property-placeholder location="classpath:jdbc.properties" /> <!--读取properties配置文件 -->

<!--读取properties的文件内容-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<bean id="dataSource" class="${dataSource}" destroy-method="close"> <!-- 配置database信息-->
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${initialSize}" /> <!-- 刚开始启动时的链接数-->
<property name="maxActive" value="${maxActive}" /> <!-- 活动量最高峰时的链接数-->
<property name="maxIdle" value="${maxIdle}" /> <!--空闲时最大链接数 -->
<property name="minIdle" value="${minIdle}" /> <!--空闲时最小的连接数 -->
</bean>
<!-- 读取xml的配置-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 生成sessionFactory并交给spring管理-->
<property name="dataSource" ref="dataSource" /> <!-- 为sessionFactory注入database-->
<property name="mappingResources"> <!--读取hibernate的配置文件 -->
<list>
<value>com/Address.hbm.xml</value> <!--配置文件-->
</list>
</property>
<property name="hibernateProperties"> <!--hibernate的配置属性 -->
<value>
hibernate.dialect=org.hibernate.dialect.SQLServerDialect <!-- 数据库所用的sql语句-->
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>

<!---读取注解的配置-->
<bean id="sf"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="ds" />

<property name="packagesToScan"> <!--注解方式扫描实体类 -->
<list>
<value>com.tr.jade.modles</value> <!-- 实体类所在的包-->
</list>
</property>
<property name="hibernateProperties"> <!-- hibernate的配置属性-->
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect <!-- 数据库所用的sql语句-->
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>

<prop key="hibernate.cache.use_second_level_cache">true</prop> <!--启用二级缓存-->
<prop key="hibernate.cache.use_query_cache">false</prop> <!--是否启动查询缓存-->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <!--指定缓存类-->
</props>
</props>
</property>
</bean>

<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!--把hibernate的sessionfactory交给spring管理-->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" /> <!-- 打开以注解的方式管理事物-->

<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.bjsxt.registration.service.*.*(..))" /> <!--定义切面-->
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> <!--给切面注入通知-->
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager"> <!--定义事物的通知-->
<tx:attributes>
<tx:method name="delete*" read-only="true" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: