Spring中关于applicationContext.xml文件的配置实例

原创文章,鼓励转载,请务必注明出处,作者:李佳

 

1.方法一, 使用TransactionProxyFactoryBean

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
                       "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean
  class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
  <property name="driverClassName">
   <value>org.hsqldb.jdbcDriver</value>
  </property>
  <property name="url">
   <value>jdbc:hsqldb:hsql://localhost</value>
  </property>
  <property name="username">
   <value>sa</value>
  </property>
  <property name="password">
   <value/>
  </property>
 </bean>
 <bean
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
  <property name="dataSource">
   <ref local="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/hibernate/UserInfo.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>
 <bean
  class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
 <bean
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" id="exampleManager">
  <property name="transactionManager">
   <ref local="transactionManager"/>
  </property>
  <property name="target">
   <ref local="exampleManagerTarget"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="set*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <bean class="com.service.spring.impl.ExampleManagerImpl" id="exampleManagerTarget">
  <property name="userinfo">
   <ref local="userinfoDAO"/>
  </property>
 </bean>
 <bean class="com.service.dao.impl.UserInfoDAOImpl" id="userinfoDAO">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
</beans>

2.方法二,使用ProxyFactoryBean

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
                       "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean
  class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
  <property name="driverClassName">
   <value>org.hsqldb.jdbcDriver</value>
  </property>
  <property name="url">
   <value>jdbc:hsqldb:hsql://localhost</value>
  </property>
  <property name="username">
   <value>sa</value>
  </property>
  <property name="password">
   <value/>
  </property>
 </bean>
 <bean
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
  <property name="dataSource">
   <ref local="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/hibernate/UserInfo.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>
 <bean
  class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
 <bean
  class="org.springframework.transaction.interceptor.TransactionInterceptor" id="txInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <bean abstract="true"
  class="org.springframework.aop.framework.ProxyFactoryBean" id="baseUserService">
  <property name="interceptorNames">
   <list>
    <value>txInterceptor</value>
   </list>
  </property>
 </bean>
 <bean id="userService" parent="baseUserService">
  <property name="proxyInterfaces">
   <value>com.bo.UserService</value>
  </property>
  <property name="target">
   <bean class="com.bo.impl.UserServiceImpl">
    <property name="userDAO">
     <ref bean="userDAO"/>
    </property>
   </bean>
  </property>
 </bean>
 <bean class="com.dao.impl.UserDAOImpl" id="userDAO">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
</beans>

3.方法三,使用BeanNameAutoProxyCreator

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
                       "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean
  class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
  <property name="driverClassName">
   <value>org.hsqldb.jdbcDriver</value>
  </property>
  <property name="url">
   <value>jdbc:hsqldb:hsql://localhost</value>
  </property>
  <property name="username">
   <value>sa</value>
  </property>
  <property name="password">
   <value/>
  </property>
 </bean>
 <bean
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
  <property name="dataSource">
   <ref local="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/hibernate/UserInfo.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>
 <bean
  class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
 <bean
  class="org.springframework.transaction.interceptor.TransactionInterceptor" id="txInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="interceptorNames">
   <list>
    <value>txInterceptor</value>
    <!-- add new Interceptor -->
   </list>
  </property>
  <!--
  <property name="beanNames">
   <value>*Service,*Manager</value>
  </property>
   -->
  <property name="beanNames">
   <list>
    <idref local="userService"/>
    <!-- add new Service -->
   </list>
  </property>
 </bean>
 <!-- Start UserService -->
 <bean class="com.bo.impl.UserServiceImpl" id="userService">
  <property name="userDAO" ref="userDAO"/>
 </bean>
 <bean class="com.dao.impl.UserDAOImpl" id="userDAO">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值