spring全注解的xml 文件该怎么写
发布网友
发布时间:2022-05-19 11:13
我来回答
共1个回答
热心网友
时间:2023-11-02 05:19
<?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/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 声名式事务①:添加常用命名空间声明 --><!-- ***********************配置DateSource和SessionFactory************************ --> <bean id="data"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=epet">
</property>
<property name="username" value="sa"></property>
<property name="password" value=""></property>
</bean>
<bean id="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="data"></ref>
</property>
<property name="mappingResources">
<list>
<value>entity/PetInfo.hbm.xml</value>
<value>entity/PetDiary.hbm.xml</value>
<value>entity/PetType.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<!-- 配置<cache usage="read-only"/>后,配置Hibernate缓存机制属性 -->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</prop>
</props>
</property>
</bean>
<!-- ***************************配置各层依赖注入关系******************************** -->
<!-- Dao层依赖于SessionFactory,SessionFactory注入Dao层 -->
<bean id="petInfoDao" class=".PetInfoDaoImpl" >
<property name="sessionFactory">
<ref bean="sf" />
</property>
</bean>
<bean id="petDiaryDao" class=".PetDiaryDaoImpl" >
<property name="sessionFactory">
<ref bean="sf" />
</property>
</bean>
<bean id="petTypeDao" class=".PetTypeDaoImpl" >
<property name="sessionFactory">
<ref bean="sf" />
</property>
</bean>
<!-- Dao层依赖于SessionFactory,SessionFactory注入Dao层 -->
<!-- Biz层依赖于Dao层,Dao层注入Biz层 -->
<bean id="petInfoBiz" class="biz.PetInfoBizImpl" >
<property name="petInfoDao">
<ref bean="petInfoDao" />
</property>
</bean>
<bean id="petDiaryBiz" class="biz.PetDiaryBizImpl">
<property name="petDiaryDao" ref="petDiaryDao" />
</bean>
<bean id="petTypeBiz" class="biz.PetTypeBizImpl">
<property name="petTypeDao" ref="petTypeDao" />
</bean>
<!-- Biz层依赖于Dao层,Dao层注入Biz层 -->
<!-- ③配置Action依赖于Biz层,Biz层注入Action -->
<bean name="/PetAction" class="web.action.PetAction">
<!-- Biz注入Action -->
<property name="petInfoBiz" ref="petInfoBiz" />
<property name="petDiaryBiz" ref="petDiaryBiz"></property>
<property name="petTypeBiz" ref="petTypeBiz"></property>
</bean>
<bean name="/DiaryAction" class="web.action.DiaryAction">
<property name="petDiaryBiz" ref="petDiaryBiz"></property>
<property name="petInfoBiz" ref="petInfoBiz"></property>
</bean>
<bean name="/DoLogAction" class="web.action.DoLogAction">
<property name="petInfoBiz" ref="petInfoBiz"></property>
</bean>
<bean name="/DoTrainingAction" class="web.action.DoTrainingAction">
<property name="petInfoBiz" ref="petInfoBiz"></property>
</bean>
<bean name="/IndexAction" class="web.action.IndexAction">
<property name="petInfoBiz" ref="petInfoBiz"></property>
<property name="petDiaryBiz" ref="petDiaryBiz"></property>
<property name="petTypeBiz" ref="petTypeBiz"></property>
</bean>
<!-- ③配置Action依赖于Biz层,Biz层注入Action -->
<!-- ****************************配置声明式事务************************************ --><!-- 声名式事务②:定义事务管理器 -->
<bean id="myHibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean><!-- 声名式事务③:定义事务通知,声明事务规则 -->
<tx:advice id="txAdvice" transaction-manager="myHibTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do.*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice><aop:config>
<!-- 声名式事务④:定义切面,指定哪些方法应用以上规则 -->
<aop:pointcut id="bizMethods" expression="execution(* biz.*.*(..))" />
<!-- 声名式事务⑤:将事务通知和切面组合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
</aop:config><!-- ******************************配置AOP方面代码******************************** --><!-- AOP实现: ①定义原Bean(实现类)
<bean id="petInfoBizTarget" class="biz.PetInfoBizImpl">
<property name="petInfoDao" ref="petInfoDao"></property>
</bean>-->
<!-- AOP实现: ②定义通知 -->
<bean id="lotteryAdvice" class="aop.LotteryAdvice">
<property name="petInfoDao" ref="petInfoDao"></property>
</bean>
<bean id="diaryAdvice" class="aop.DiaryAdvice">
<property name="petInfoDao" ref="petInfoDao"></property>
</bean> <!-- AOP实现: ③定义代理类 -->
<bean id="petInfoBizProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 被代理的接口 -->
<property name="proxyInterfaces">
<list>
<value>biz.PetInfoBiz</value>
<value>biz.PetDiaryBiz</value>
</list>
</property>
<!-- 织入的通知列表 -->
<property name="interceptorNames">
<list>
<value>lotteryAdvice</value>
<value>diaryAdvice</value>
</list>
</property>
<!-- 被代理的原Bean -->
<property name="target">
<list>
<ref bean="petInfoBiz"/>
<ref bean="petDiaryBiz"/>
</list>
</property>
</bean></beans>