spring基于注解的配置文件

  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" xmlns:context="http://www.springframework.org/schema/context"
  4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  7                         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  8                         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
  9     default-lazy-init="true">
 10     <!-- 读取jdbc.properties中的配置信息-->
 11     <bean id="propertyConfigurer"
 12         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 13         <property name="location">
 14             <value>classpath:jdbc.properties</value>
 15         </property>
 16     </bean>
 17     <!-- 配置dataSource,从jdbc.properties中取出参数-->
 18     <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 19         <property name="driverClass" value="com.mysql.jdbc.Driver" />
 20         <property name="jdbcUrl" value="${jdbc.url}" />
 21         <property name="user" value="${jdbc.username}" />
 22         <property name="password" value="${jdbc.password}" />
 23         <property name="autoCommitOnClose" value="true" />
 24         <property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
 25         <property name="initialPoolSize" value="${cpool.minPoolSize}" />
 26         <property name="minPoolSize" value="${cpool.minPoolSize}" /><!-- 最小连接数 -->
 27         <property name="maxPoolSize" value="${cpool.maxPoolSize}" /><!-- 最大连接数 -->
 28         <property name="maxIdleTime" value="${cpool.maxIdleTime}" /><!-- 最大闲置时间 秒-->
 29         <!-- 达 到最大连接数后可以增加的连接数 个 -->
 30         <property name="acquireIncrement" value="${cpool.acquireIncrement}" />
 31         <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
 32         <property name="testConnectionOnCheckin" value="${cpool.testConnectionOnCheckin}" />
 33         <property name="automaticTestTable" value="${cpool.automaticTestTable}" />
 34         <!-- 闲 置的连接测试周期 (秒) -->
 35         <property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}" />
 36         <property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}" />
 37     </bean>
 38     <!-- sessionFactory包含了数据库的配置,并映射了实体类 -->
 39     <bean id="sessionFactory"
 40         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 41         <!-- 引入数据源 -->
 42         <property name="dataSource" ref="ds" />
 43         <!-- 加载HBM映射文件-->
 44         <property name="mappingLocations">
 45             <list>
 46                 <value>classpath:com/leadtone/**/*.hbm.xml</value>
 47                 <value>classpath:com/wangzhongsoft/**/*.hbm.xml</value>
 48             </list>
 49         </property>
 50         <!-- 通过属性文件配置hibernate -->
 51         <property name="hibernateProperties">
 52             <value>
 53                 hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
 54                 hibernate.show_sql=false
 55                 hibernate.format_sql=false
 56                 hibernate.query.substitutions=true 1, false 0
 57                 hibernate.jdbc.batch_size=20
 58                 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
 59                 hibernate.cache.provider_configuration_file_resource_path=/ehcache-hibernate.xml
 60                 hibernate.cache.use_second_level_cache=false<!-- 二级缓存配置 -->
 61             </value>
 62         </property>
 63         <!-- 正式运行时需要去除相应项 -->
 64         <!-- hibernate.generate_statistics 如果开启, Hibernate将收集有助于性能调节的统计数据.  -->
 65         <!--
 66             hibernate.use_sql_comments 如果开启, Hibernate将在SQL中生成有助于调试的注释信息,
 67             默认值为false. .
 68         -->
 69     </bean>
 70     <!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 (session会自动关闭)-->
 71     <bean id="transactionManager"
 72         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 73         <!-- 为事务管理器注入sessionFactory -->
 74         <property name="sessionFactory" ref="sessionFactory" />
 75     </bean>
 76 
 77 
 78     <!-- JDBC 用于自动注入 -->
 79     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 80         <constructor-arg ref="ds" />
 81     </bean>
 82 
 83 
 84     <!-- JDBC 用于自动注入 -->
 85     <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
 86         <constructor-arg ref="jdbcTemplate" />
 87     </bean>
 88     <!-- 向spring容器注册相应的注解方式 使容器识别相应的注解方式-->
 89     <context:annotation-config />
 90     <!-- 扫描文件夹自动注解 以便进行解析-->
 91     <context:component-scan base-package="com.leadtone" />
 92     <context:component-scan base-package="com.wangzhongsoft" />
 93     <!-- 支持@Transactional标记 注解方式管理事务 开启事务行为-->
 94     <tx:annotation-driven transaction-manager="transactionManager" />
 95 
 96     <!-- 以AspectJ方式定义 AOP -->
 97     <aop:aspectj-autoproxy />
 98 
 99 
100     <!-- HibernateTemplate 代码开发要用到的与数据库操作的类 -->
101     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
102         <property name="sessionFactory">
103             <ref bean="sessionFactory" />
104         </property>
105     </bean>
106 
107     <!--
108         加载其它的 applicationContext.xml 配置文件 <import resource="classpath:*.xml"/>
109     -->
110     
111     <!-- 加载定时任务,正式上线时需开启
112     <import resource="classpath:QuartzConfig.xml"/>-->
113 
114 </beans>
原文地址:https://www.cnblogs.com/shaohz2014/p/4461942.html