hibernate3.3:基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)

  基于AnnotationSSH整合开发其实并没有我当初想像中那么顺利真正去做时候才发觉有许多问题但不要紧探索下吧在探索过程中学到知识才是最重要

   言归正传现在我们加入Spring支持:把spring-framework-2.5.5dist中spirng.jar引进我们项目lib目录来还要添加libaspectj下两个jar包以支持切面编程

   必要配置文件还是要:   applicationContext-common.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:context="http://www.springframework.org/schema/context"
    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.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
           
    <!-- 配置SessionFactory,由Spring容器来管理Hibernate -->
    <!-- 非Annotation时,使用org.springframework.orm.hibernate3.LocalSessionFactoryBean,
        它注入实体类方式是MappingResources,而Hibernate Annotation所用映射方式
        不是mapping resource,而是mapping ,这就要用到LocalSessionFactoryBean子类
        AnnotationSessionFactoryBean了.AnnotationSessionFactoryBean它支持实体注入
        方式AnnotatedClasses,即对应Hibernate中mapping .参见这两个类源代码. -->
    <bean id="sessionFactory"
        ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="configLocation">
            <value>path:hibernate.cfg.xml</value>
        </property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        ="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
   
    <!-- 配置事务传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
   
   
    <!-- 那些类哪些思路方法参和事务 -->
    <aop:config>
        <aop:pocut id="allServiceMethod" expression="execution(* com.rong.dao.*.*.*(..))" />
        <aop:advisor pocut-ref="allServiceMethod" advice-ref="txAdvice" />
    </aop:config>
   
    <!-- 使Spring关注Annotation -->
    <context:annotation-config/>
   
    <!-- 让Spring通过自动扫描来查询和管理Bean -->
    <context:component-scan base-package="com.rong"/>
   
    <!--
    <bean id="userDao" ="com.rong.dao.UserDaoBean">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="userService" ="com.rong.service.UserServiceBean">
        <property name="userDao" ref="userDao"/>
    </bean>
     -->
   
</beans>


   关键两点:

    <!-- 使Spring关注Annotation -->
    <context:annotation-config/>
   
    <!-- 让Spring通过自动扫描来查询和管理Bean -->
    <context:component-scan base-package="com.rong"/>


   这样配置的后就省去了上面注释掉DAO层和Service层等配置代码是不是很方便呢

   有关这部分XML代码我们下面还会作解释

   来开发我们DAO层吧,接口如下:

package com.rong.dao;

import java.util.List;
import com.rong.entity.User;

public erface UserDao {
   
    public void save(User user);
   
    public void delete( id);
   
    public void update(User user);
   
    public List<User> query;
   
    public User get( id);

}


   DAO层实现类:

package com.rong.dao;

import java.util.List;
import org.springframework.stereotype.Repository;
import com.rong.entity.User;

@Repository("userDao")        //声明此类为数据持久层
public UserDaoBean extends MyHibernateDaoSupport implements UserDao {
   
    public void save(User user){
        super.getHibernateTemplate.save(user);
    }
   
    public void delete( id){
        super.getHibernateTemplate.delete(super.getHibernateTemplate.load(User., id));
    }
   
    public void update(User user){
        super.getHibernateTemplate.update(user);
    }
   
    @SuppressWarnings("unchecked")
    public List<User> query{
        super.getHibernateTemplate.find("from User");
    }
   
    public User get( id){
        (User)super.getHibernateTemplate.get("from User", id);
    }

}


   大家可以看到我们这里继承不是HibernateDaoSupport,而是我自己编写个类MyHibernateDaoSupport其代码如下:

package com.rong.dao;

import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public MyHibernateDaoSupport extends HibernateDaoSupport {
   
    @Resource(name="sessionFactory")    //为父类HibernateDaoSupport注入sessionFactory
    public void SuperSessionFactory(SessionFactory sessionFactory){
        super.SessionFactory(sessionFactory);
    }

}    




  我们的所以要改写HibernateDaoSupport是因我为我们要为DAO层类注入SessionFactory这个属性以后我们开发DAO类就可以直接重用这个MyHibernateDaoSupport了其实这样做是相当于配置文件方式代码:          

   <bean id="userDao" ="com.rong.dao.UserDaoBean">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>       


  我们既然要用annotation代替XML文件就要让它也能像原来那样使用sessionFactory,故为MyHibernateDaoSupport注入SessionFactory子类继承这个类时也继承其Annotation这样我们就可以实现SessionFactory注入了

   到现在我们再回过头来看applicationContext-common.xml中

    <bean id="sessionFactory"
        ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="configLocation">
            <value>path:hibernate.cfg.xml</value>
        </property>
    </bean>    




   我们平时开发Hibernate和Spring整合时常常会用到org.springframework.orm.hibernate3.LocalSessionFactoryBean来提供SessionFactory而我们这里却要改成org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean其实是这样我们在Hibernate.cfg.xml中配置实体类映射方式如下:(详见基于AnnotationStruts2.0+Hibernate3.3+Spring2.5整合开发 (1) )

        <!--
        <mapping resource="com/rong/entity/User.hbm.xml"/>
         -->
        
         <!-- 在Hibernate中注册User实体类,区别于上面注释掉resource写法 -->
         <mapping ="com.rong.entity.User"/>   




  要使Hibernate实体类支持注解去掉xxx.hbm.xml文件故我们所用是mapping 方式不是mapping resource思路方法然而LocalSessionFactoryBean这个类它采用实体类映射方式是mapping resource(详情可参见LocalSessionFactoryBean这个类源代码)如果我们在配置中仍然用这个类Hibernate和Spring整合时就会报错而AnnotationSessionFactoryBean这个类在LocalSessionFactoryBean基础上添加了mapping 方式实现实体类映射(详见AnnotationSessionFactoryBean类源代码)

   我们再来看Service层代码:(接口比较简单节约篇幅就不列出了)

package com.rong.service;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rong.dao.UserDao;
import com.rong.entity.User;

@Service("userService")        //声明此类为业务逻辑层
public UserServiceBean implements UserService {
   
    @Autowired
    private UserDao userDao;

    public void save(User user){
        userDao.save(user);
    }

} 




  我们用到注解上面般都作了注释就不多叙@Autowired和@Resource功能差不多就是把对象注入相当于<bean>配置功能

   好就开发到这样是不是忘记了什么?记得要配置web.xml部分代码如下:

      <!-- 修改Spring配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>path*:applicationContext-*.xml</param-value>
    </context-param>
   
    <!-- 配置Spring -->
    <listener>
        <listener->org.springframework.web.context.ContextLoaderListener</listener->
    </listener>




   是不是真成功了?用Junit测试下吧我测试过是没问题由于篇幅Junit测试代码就不贴出来了自己练习下吧!

   其实到现在为止我们发觉我们XML配置文件还是很多其实这样想想阶段我们省去了xxx.hbm.xml这类文件阶段我们少去了<bean id="" =""><property name="" ref="">这样配置项而这些正是我们项目开发中大量使用配置而只要书写简单Annotation注解就可以省去这样我们何乐而不用而那些我们保留XML配置文件(如:数据库连接事务)这样是写死个项目就写次或复制过来用我们保留它又何妨?

   好暂时到这里我们还有下阶段基于AnnotationSSH整合开发我们将会以个用户注册例子把Struts2注解带到我们整合开发中来起期待吧!

  出处 http://www.blogjava.net/rongxh7

  系列文章:

  基于AnnotationStruts2.0+Hibernate3.3+Spring2.5整合开发 (1)



Tags:  spring2.5 struts2.0 hibernate3.3.2 hibernate3.3

延伸阅读

最新评论

发表评论