`

[转]引用 MyEclipse中applicationContext.xml配置及常见问题

    博客分类:
  • ssh
 
阅读更多
引用 MyEclipse中applicationContext.xml配置及常见问题
2011-05-21 12:21

 

SHH(Struts1.2 + Spring2.0 + hibernate3.1)结合的javaWeb工程的applicationContext.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/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">

 <!-- 会话工厂 -->
 <bean id="SessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation" value="classpath:hibernate.cfg.xml">
  </property>
 </bean>
 <!-- 数据层 -->
 <bean id="dao" class="dao.DaoSupportHibernate3Impl">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 <!-- 业务层 -->
 <bean id="city" class="service.CityServiceImpl">
  <property name="support" ref="dao"></property>
 </bean>
 
 <!-- 事务管理 -->
 <bean id="myHibTransactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <!-- 事务通知 -->
 <tx:advice id="txAdvice" transaction-manager="myHibTransactionManager">
  <tx:attributes>
   <tx:method name="*" propagation="REQUIRED" />
  </tx:attributes>
 </tx:advice>
 
 <!-- 添加事务 -->
 <aop:config>
  <aop:pointcut id="bizMethods" expression="execution(* com.service.*.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
 </aop:config>
</beans>

以上是整个文件的具体配置,Spring使用 <tx:advice>和 <aop:config> 用来配置事务,具体如何配置你可以参考Spring文档。

(* com.service.*.*(..))中几个通配符的含义:

第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.service下的任意class
第三个 * —— 通配 包com.service下的任意class的任意方法
第四个 .. —— 通配 方法可以有0个或多个参数

 

此文件会时常出现 Class"org.springframework.orm.hibernate3.LocalSessionFactoryBean"not found和The prefix "tx" for element "tx:advice" is not bound两个问题。

第一个问题解决办法:

出现该问题是在为工程添加Spring包的时候没有添加Spring 2.0 Persistence Core Libraries一项,导致缺少Spring的spring-hibernate3.jar包。

第二个问题解决办法:

出现该问题是定义申明AOP的时候,没有加载schema,只需要在<beans>中要加入“xmlns:aop”的命名申明,并在“xsi:schemaLocation”中指定aop配置的schema的地址。
配置文件如下:
<?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.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop.xsd ">


问题到此解决完。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics