c3p0、spring,同时hibernate也是注入的

applicationContext-resources.xml文件:

第一个是我的项目写的

<?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: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.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/cetc?characterEncoding=UTF8"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>          
            <property name="maxPoolSize" value="200"></property>
            <property name="minPoolSize" value="10"></property>
            <property name="initialPoolSize" value="10"></property>
            <property name="maxIdleTime" value="20"></property> 
            <property name="acquireIncrement"><value>5</value></property>
        </bean>
      
        
</beans>

第二个是比较详细的,仅供参考,没必要全配

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
            [url]http://www.springframework.org/schema/jee[/url] [url]http://www.springframework.org/schema/jee/spring-jee-2.0.xsd[/url]">
    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="jdbcUrl">
            <value>jdbc:mysql://192.168.3.110:3306/DBName?useUnicode=true&characterEncoding=GBK</value>
        </property>
        <property name="user">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
 
 <!--连接池中保留的最小连接数。-->
        <property name="minPoolSize">
            <value>5</value>
        </property>
 
 <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">
            <value>30</value>
        </property>
 
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize">
            <value>10</value>
        </property>
 
 <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime">
            <value>60</value>
        </property>
 
 <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement">
            <value>5</value>
        </property>
 
 <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
  如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
        <property name="maxStatements">
            <value>0</value>
        </property>
 
 <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">
            <value>60</value>
        </property>
 
 <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts">
            <value>30</value>
        </property>
 
 <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
  保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
  获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
        <property name="breakAfterAcquireFailure">
            <value>true</value>
        </property>
 
 <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
  时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
  等方法来提升连接测试的性能。Default: false -->
        <property name="testConnectionOnCheckout">
            <value>false</value>
        </property>
    </bean>
applicationContext-hibernate.xml文件:

<?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: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.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                 <!-- 开始-->
                 <value>com/infomanager/hbm/DepDTO.hbm.xml</value>
                 <value>com/infomanager/hbm/RoleDTO.hbm.xml</value>
                 <value>com/infomanager/hbm/RoleFunctionDTO.hbm.xml</value>
                 <value>com/infomanager/hbm/UserRole.hbm.xml</value>
                 <!-- 结束 -->
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">com.infomanager.util.ZdyMySQLDialect</prop>
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
                <prop key="hibernate.jdbc.batch_size">0</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    
    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>
如果想把hibernateProperties配的比较全面,可以如下:

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
                <prop key="hibernate.autoReconnect">true</prop>
            </props>
        </property>

配置tx/aop声明式事物

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
</bean>
<!-- 配置tx/aop声明式事务 -->
<!-- 声明一个切面 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
<!-- tx:attributes>
<tx:method name="find*" propagation="REQUIRED"
read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes-->
</tx:advice>

<!-- 把切面注入到业务中 -->
<aop:config>
<aop:pointcut id="affectMethods"
expression="execution(* com.keer.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="affectMethods" />
</aop:config>
还有另一种,就是我们公司的这个项目,看applicationContext-hibernate.xml文件,我的transactionManager,放在此文件中,然后在每一个applicationContext-yzService.xml中都配一个事物通知、切入点,然后把通知和切入点结合,就是织入

<aop:config>
        <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))" order="2"/>
    </aop:config>
    
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
这样竟然也行,但是我认为应该这样,就是在通知中加transaction-manager="transactionManager",如下所示:

<aop:config>
		<aop:advisor id="managerTx" advice-ref="txAdvice"
			pointcut="execution(* *..service.*Manager.*(..))" order="2" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>
ok,最后我总结一下service依赖dao,dao依赖sessionFactory,sessionFactory依赖dataSource、mappingResources、hibernateProperties,在mappingResources中有一个list放表的映射文件,例如:

 <list>
				 <value>com/infomanager/hbm/FunctionDTO.hbm.xml</value>
                 <value>com/infomanager/hbm/IappUserDTO.hbm.xml</value>
</list>
hibernateProperties,就放一些key--value,例如:

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">com.infomanager.util.ZdyMySQLDialect</prop>
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
                <prop key="hibernate.jdbc.batch_size">0</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
重点, dataSource,我们举c3p0的例子:

 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/cetc?characterEncoding=UTF8"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>          
            <property name="maxPoolSize" value="200"></property>
            <property name="minPoolSize" value="10"></property>
            <property name="initialPoolSize" value="10"></property>
            <property name="maxIdleTime" value="20"></property> 
            <property name="acquireIncrement"><value>5</value></property>
        </bean>


Pointcut是Join Point的集合,它是程序中需要注入Advice的位置的集合,指明Advice要在什么样的条件下才能被触发。

org.springframework.aop.Pointcut 接口用来指定通知到特定的类和方法。查看Spring下载包里的源文件Pointcut.java,路径是spring-framework-2.0-m1/src/org/springframework/aop,可以看到Pointcut.java.源代码如下:

//******* Pointcut.java**************

package org.springframework.aop;

public interface Pointcut {

         //用来将切入点限定在给定的目标类中

         ClassFilter getClassFilter();

         //用来判断切入点是否匹配目标类给定的方法

         MethodMatcher getMethodMatcher();

         Pointcut TRUE = TruePointcut.INSTANCE;

}

代码说明:

  ●       接口ClassFilter,用来将切入点限定在给定的目标类中。

  ●       接口MethodMatcher,用来判断切入点是否匹配目标类给定的方法。

从上面可以看出,在接口Pointcut中,主要包含两个接口:ClassFilter和MethodMatcher,有利于代码的重用。

通知(Advice)

Advice是某个连接点所采用的处理逻辑,也就是向连接点注入的代码。前面示例中提取出来输出日志信息的代码就是一个Advice,表示要在Join Point加入这段代码。

Advisor

Advisor是Pointcut和Advice的配置器,它包括Pointcut和Advice,是将Advice注入程序中Pointcut位置的代码。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值