<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:repository="http://www.springframework.org/schema/data/repository"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
		http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
	default-lazy-init="true">
	
	<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />

	<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->  
    <context:component-scan base-package="cn.com.liandisys.ycjt.ggcx.*">  
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    </context:component-scan>  
    
    <!--处理 @ResponseBody 中文乱码问题-->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>
								text/html;charset=UTF-8
							</value>
						</list>
					</property>
				</bean>
				<!-- 启动JSON格式的配置 -->
				<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
					<!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation -->
					<property name="supportedMediaTypes">
						<list>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>
  
    <!-- JPA实体管理工厂的配置 -->  
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">   
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="cn.com.liandisys.ycjt.ggcx.*"/><!--待扫描的实体类包，不再需要persistence.xml了-->  
        <property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
		</property>
        <property name="jpaProperties">  
            <props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">false</prop>
			</props> 
        </property>  
    </bean>
	
    <!--指定实现JPA的适配器-->  
    <bean id="hibernateJpaVendorAdapter"
		class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
   
    <!-- 重要配置：启用扫描并自动创建代理的功能  -->
    <jpa:repositories base-package="cn.com.liandisys" 
        entity-manager-factory-ref="entityManagerFactory" 
        transaction-manager-ref="transactionManager"/>
    
    <!-- Jpa 事务配置 -->  
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
        <property name="entityManagerFactory" ref="entityManagerFactory"/>  
    </bean>  
   
    <!-- Hibernate事务配置 -->
	<bean id="hibernate-transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
    <!-- 使用annotation定义事务 -->  
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />  
  
    <!-- JSR303 Validator定义 -->  
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />  
      
    <bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingLocations" value="classpath:*.hbm.xml" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">false</prop>
				<prop key="hibernate.jdbc.fetch_size">50</prop>
				<prop key="hibernate.jdbc.batch_size">25</prop>
				<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
			</props>
		</property>
	</bean>
    
    <!-- Spring jdbcTemplate配置 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
    
	
    <!-- 数据源配置, 使用DBCP数据库连接池 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
        <!-- Connection Info -->  
        <property name="driverClassName" value="${jdbc.driver}" />  
        <property name="url" value="${jdbc.url}" />  
        <property name="username" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
      
        <!-- Connection Pooling Info -->  
        <property name="maxActive" value="${dbcp.maxActive}" />  
        <property name="maxIdle" value="${dbcp.maxIdle}" />  
        <property name="defaultAutoCommit" value="false" />  
    </bean>  
    
</beans>  