spring配置时的bean注入

来源:互联网 发布:知非什么意思 编辑:程序博客网 时间:2024/06/06 07:46
<?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:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        destroy-method="close">        <property name="driverClassName">            <value>com.mysql.jdbc.Driver</value>        </property>        <property name="url">            <value>jdbc:mysql://localhost:3306/ssh2</value>        </property>        <property name="username">            <value>root</value>        </property>        <property name="password">            <value>123456</value>        </property>    </bean>    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource">         <ref local="dataSource"/>        </property>        <property name="mappingResources">         <list>         <value>com/test/bean/User.hbm.xml</value>         </list>        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">                    org.hibernate.dialect.MySQLDialect                </prop>                <prop key="hibernate.show_sql">true</prop>            </props>        </property>    </bean>    <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">        <property name="sessionFactory">            <ref bean="sessionFactory" />        </property>    </bean>    <bean id="userServiceTarget" class="com.test.service.impl.UserServiceImpl">        <property name="userDao" ref="userDao"></property>    </bean>        <bean id="IsshWs" class="com.test.wsservice.impl.SshWsImpl">        <property name="userServiceTarget" ref="userService"></property>    </bean>        <jaxws:endpoint implementor="#IsshWs" address="/cxfssh.ws">    </jaxws:endpoint>        <bean id="userService"        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">        <property name="target" ref="userServiceTarget"></property>        <property name="transactionManager" ref="transactionManager"></property>        <property name="transactionAttributes">            <props>                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>                <prop key="*">PROPAGATION_REQUIRED</prop>            </props>        </property>    </bean></beans>
1.事务管理:每个Bean都有一个代理(比较笨拙的配置)

     dataSource 注入进sessionFactory,将sessionFactory注入到transactionManager(org.springframework.orm.hibernate3.HibernateTransactionManager这是一个事务管理器,声明式事务), 再注入userService(TransactionProxyFactoryBean实现spring声明式事务)userService的属性transactionManager是为事务代理bean注入事务管理器如:

(用其他例子来说明)

2.spring的bean注入:

          <!--sessionFactory注入到事务管理器中--><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean>        <!--sessionFactory注入到dao层 ,同时dao层要设置成单例--><bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="userServiceTarget" class="com.test.service.impl.UserServiceImpl"><property name="userDao" ref="userDao"></property></bean>       <!--将配置有事务userService注入到SshWsImpl类 userServiceTarget属性中--><bean id="IsshWs" class="com.test.wsservice.impl.SshWsImpl"><property name="userServiceTarget" ref="userService"></property></bean><jaxws:endpoint implementor="#IsshWs" address="/cxfssh.ws"></jaxws:endpoint><bean id="userService"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="target" ref="userServiceTarget"></property><property name="transactionManager" ref="transactionManager"></property><property name="transactionAttributes"><props><prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean>///sessionFactory注入到dao层,dao注入到service层,service层配置好事务注入到action层或是接口层,完成。///sessionFactory注入到事务管理器transactionManager,transactionManager再注入到TransactionProxyFactoryBean,完成事务配置。



0 0