spring基于注解的配置文件

时间:2022-02-16 19:16:56
 <?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"
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"
default-lazy-init="true">
<!-- 读取jdbc.properties中的配置信息-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- 配置dataSource,从jdbc.properties中取出参数-->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
<property name="initialPoolSize" value="${cpool.minPoolSize}" />
<property name="minPoolSize" value="${cpool.minPoolSize}" /><!-- 最小连接数 -->
<property name="maxPoolSize" value="${cpool.maxPoolSize}" /><!-- 最大连接数 -->
<property name="maxIdleTime" value="${cpool.maxIdleTime}" /><!-- 最大闲置时间 秒-->
<!-- 达 到最大连接数后可以增加的连接数 个 -->
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
<property name="testConnectionOnCheckin" value="${cpool.testConnectionOnCheckin}" />
<property name="automaticTestTable" value="${cpool.automaticTestTable}" />
<!-- 闲 置的连接测试周期 (秒) -->
<property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}" />
<property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}" />
</bean>
<!-- sessionFactory包含了数据库的配置,并映射了实体类 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource" ref="ds" />
<!-- 加载HBM映射文件-->
<property name="mappingLocations">
<list>
<value>classpath:com/leadtone/**/*.hbm.xml</value>
<value>classpath:com/wangzhongsoft/**/*.hbm.xml</value>
</list>
</property>
<!-- 通过属性文件配置hibernate -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.query.substitutions=true 1, false 0
hibernate.jdbc.batch_size=20
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.provider_configuration_file_resource_path=/ehcache-hibernate.xml
hibernate.cache.use_second_level_cache=false<!-- 二级缓存配置 -->
</value>
</property>
<!-- 正式运行时需要去除相应项 -->
<!-- hibernate.generate_statistics 如果开启, Hibernate将收集有助于性能调节的统计数据. -->
<!--
hibernate.use_sql_comments 如果开启, Hibernate将在SQL中生成有助于调试的注释信息,
默认值为false. .
-->
</bean>
<!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 (session会自动关闭)-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 为事务管理器注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- JDBC 用于自动注入 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="ds" />
</bean> <!-- JDBC 用于自动注入 -->
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="jdbcTemplate" />
</bean>
<!-- 向spring容器注册相应的注解方式 使容器识别相应的注解方式-->
<context:annotation-config />
<!-- 扫描文件夹自动注解 以便进行解析-->
<context:component-scan base-package="com.leadtone" />
<context:component-scan base-package="com.wangzhongsoft" />
<!-- 支持@Transactional标记 注解方式管理事务 开启事务行为-->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 以AspectJ方式定义 AOP -->
<aop:aspectj-autoproxy /> <!-- HibernateTemplate 代码开发要用到的与数据库操作的类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean> <!--
加载其它的 applicationContext.xml 配置文件 <import resource="classpath:*.xml"/>
--> <!-- 加载定时任务,正式上线时需开启
<import resource="classpath:QuartzConfig.xml"/>--> </beans>