spring的配置

    技术2022-05-20  44

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- hibernate 设置 --> <bean id="hbFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close"> <property name="configLocation"> <!-- <value>file:src/hibernate.cfg.xml</value> --> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="transaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="hbFactory"></ref> </property> </bean> <!-- 接口类,引用实现类并设置参数 --> <bean id="LeeDataDAO" class="lee.imp.LeeDataIMP"> <property name="sessionFactory"> <ref local="hbFactory"/> </property> </bean> <bean id="LeeAutokeyDAO" class="lee.imp.LeeAutokeyIMP"> <property name="sessionFactory"> <ref local="hbFactory"/> </property> </bean> <!-- 事务代理类 --> <bean id="LeeDataProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="target"> <ref local="LeeDataDAO"/> </property> <property name="transactionManager"> <ref local="transaction"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="LeeAutokeyProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="target"> <ref local="LeeAutokeyDAO"/> </property> <property name="transactionManager"> <ref local="transaction"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> </beans>

     

     

    spring的调用 

     

     package myutils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class SpringContext { // private static ApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/classes/applicationContext.xml"); private static ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); // private static ApplicationContext context = new FileSystemXmlApplicationContext("E:/Lee/Tomcat/Tomcat55/webapps/wwfck/WEB-INF/applicationContext.xml"); public static ApplicationContext getContext(){ return context; } }

    调用方法

    LeeDataDAO lddao = (LeeDataDAO)SpringContext.getContext().getBean("LeeDataProxy");


    最新回复(0)