<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--
<context:annotation-config/>他的作用是隐式地向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor(使用@Autowired注解)、
CommonAnnotationBeanPostProcessor(使用@ Resource 、@ PostConstruct、@ PreDestroy)、
PersistenceAnnotationBeanPostProcessor(使用@PersistenceContext注解)、
RequiredAnnotationBeanPostProcessor(使用 @Required的注解)
这 4 个BeanPostProcessor。
传统注入:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
...
-->
<context:annotation-config />
<!-- 使用注解一般都会配置扫描包路径选项
该配置项其实也包含了自动注入上述processor的功能,
因此当使用 <context:component-scan/> 后,
就可以将 <context:annotation-config/> 移除了。
-->
<context:component-scan base-package="cn.sd.erp" />
<!--
加载properties文件.
如果想要配置多个properties文件
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
这种方式是不被允许的,一定会出"Could not resolve placeholder"。
解决方案:
(1) 在Spring 3.0中,可以写:
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
(2) 但是在Spring 2.5中,<context:property-placeholder>没有ignore-unresolvable属性,所以就不能使用上面的那种方法去配置,可以改如下的格式:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
-->
<context:property-placeholder location="/WEB-INF/jdbc_hibernate.properties"/>
<!-- 配置数据数据库连接池这里使用c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<property name="acquireIncrement"
value="${jdbc.acquireIncrement}" />
<property name="idleConnectionTestPeriod" value="60"></property>
</bean>
<!--
1.关于AnnotationSessionFactoryBean和LocalSessionFactoryBean的区别.
annotation是注释、注解 的意思,在hibernate配置实体及字段与表的映射时,
annotationSessionFactoryBean提供一种注解的方式来映射,
它可以依赖@注解通过实体类生成表(包括字段、主键···)。
这种方法是spring2.5以后才有的。
2.LocalSessionFactoryBean
在applicationContext.xml中配置之后,需要另外一个***.hbm.xml的配置文件对实体和表的具体映射进行配置。
<property name="mappingLocations">
<list>
<value>classpath:/cn/cuit/crm/domain/*.hbm.xml</value>
</list>
</property>
3.如果使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,还需要配置
<property name="annotatedClasses">
<list>
<value>com.cddgg.jcoa.domain.authority.Department</value>
<value>com.cddgg.jcoa.domain.authority.Log</value>
...
</list>
<property>
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath:/cn/sd/erp/domain/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.hbm2ddl.auto">
${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.format_sql">
${hibernate.format_sql}
</prop>
</props>
</property>
</bean>
<!-- 申明一个事务管理容器,这个容器里面需要注入sessionFactory -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务策略:对于具体方法进行开启事务 -->
<tx:advice id="crudAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- AOP配置 -->
<aop:config>
<!-- 切点定义,对哪些类进行切点 -->
<aop:pointcut expression="execution(* cn.sd.crm.erp..*Service*.*(..))" id="crudPointCut"/>
<aop:advisor advice-ref="crudAdvice" pointcut-ref="crudPointCut"/>
</aop:config>
</beans>