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

Spring+hibernate 配置文件

2017-08-03 15:12 330 查看
<?xml version="1.0" encoding="UTF-8"?>

<!-- 配置xml需要的schema -->
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 
<!-- 扫描包下面的所有的类是否受spring的管理,需要spring创建对象 -->
<context:component-scan base-package="com.ac.test">
<!-- 不包含@Controller注解和@ControllerAdvice注解(配置全局异常处理的)
(因为spring-mvc里面已经扫描并管理了,所以为了避免重复管理,应该排除) -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 配置config.properties的文件路径 -->
<context:property-placeholder location="classpath:config.properties" />

<!-- 配置数据库和连接池信息,使用config.properties里面的值 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>

<!-- 初始化时获取三个连接(取值应在minPoolSize与maxPoolSize之间。默认值: 3) -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<!-- 连接池中保留的最小连接数,默认值:3 -->
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
<!-- 连接池中保留的最大连接数,默认值:15 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<!-- 当连接池中的连接数耗尽的时候,c3p0一次同时获取的连接数,默认值:3 -->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="${jdbc.maxStatements}"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="${jdbc.maxStatementsPerConnection}"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}"></property>
</bean>

<!-- 配置Hibernate的sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置Hibernate实体类的路径(通过扫描的方式配置)允许使用通配符,例如:com.ac.test.*.entity -->
<property name="packagesToScan">
<list>
<value>com.ac.test.entity</value>
</list>
</property>
<!-- 配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<!-- 配置国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="msg"></property>
</bean>

<!-- 声明式事务配置 -->

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!-- 配置通知(配置事务执行的方法定义的名称规则) -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 配置AOP的规则 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="pc" expression="execution(* com.ac.test.biz.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="myAdvice" />
</aop:config>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: