基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
基于Annotation的SSH整合开发,其实,并没有我当初想像中那么顺利。真正去做的时候,才发觉有许多问题。但不要紧,探索一下吧。在探索过程中学到知识,才是最重要的。
言归正传,现在,我们加入Spring的支持:把spring-framework-2.5.5\dist中的spirng.jar引进我们项目的lib目录来,还要添加\lib\aspectj\下的两个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,
它注入实体类的方式是setMappingResources(),而Hibernate Annotation所用的映射方式
不是mapping resource,而是mapping class,这就要用到LocalSessionFactoryBean的子类
AnnotationSessionFactoryBean了.因为AnnotationSessionFactoryBean它支持实体的注入
方式setAnnotatedClasses,即对应Hibernate中的mapping class.参见这两个类的源代码. -->
<bean id="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:pointcut id="allServiceMethod" expression="execution(* com.rong.dao.*.*.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
</aop:config>
<!-- 使Spring关注Annotation -->
<context:annotation-config/>
<!-- 让Spring通过自动扫描来查询和管理Bean -->
<context:component-scan base-package="com.rong"/>
<!--
<bean id="userDao" ref="sessionFactory"/>
</bean>
<bean id="userService" 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 interface UserDao {
public void save(User user);
public void delete(int id);
public void update(User user);
public List<User> query();
public User get(int id);
}
DAO层的实现类:
package com.rong.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.rong.entity.User;
@Repository("userDao") //声明此类为数据持久层的类
public class UserDaoBean extends MyHibernateDaoSupport implements UserDao {
public void save(User user){
super.getHibernateTemplate().save(user);
}
public void delete(int id){
super.getHibernateTemplate().delete(super.getHibernateTemplate().load(User.class, id));
}
public void update(User user){
super.getHibernateTemplate().update(user);
}
@SuppressWarnings("unchecked")
public List<User> query(){
return super.getHibernateTemplate().find("from User");
}
public User get(int id){
return (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 class MyHibernateDaoSupport extends HibernateDaoSupport {
@Resource(name="sessionFactory") //为父类HibernateDaoSupport注入sessionFactory的值
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
} 我们之所以要改写HibernateDaoSupport,是因我为,我们要为DAO层的类注入SessionFactory这个属性。以后,我们开发的DAO类,就可以直接重用这个MyHibernateDaoSupport了。其实,这样做是相当于配置文件方式的代码: <bean id="userDao" ref="sessionFactory"/>
</bean> 我们既然要用annotation代替XML文件的,就要让它也能像原来那样使用sessionFactory,故为MyHibernateDaoSupport注入SessionFactory。子类继承这个类时,也继承其Annotation。这样,我们就可以实现SessionFactory的注入了。
到现在,我们再回过头来看applicationContext-common.xml中的
<bean id="sessionFactory"
ref="">这样的配置项。而这些,正是我们项目开发中,大量使用的配置。而只要书写简单的Annotation注解,就可以省去这样,我们何乐而不用。而那些我们保留的XML配置文件(如:数据库连接,事务),这样是写死的,一个项目就写一次或复制过来用,我们保留它又何妨?
好,暂时到这里,我们还有下一阶段的基于Annotation的SSH整合开发,我们将会以一个用户注册的例子,把Struts2的注解带到我们的整合开发中来。一起期待吧!
(*^-^*) 本文原创,转载请注明出处, http://www.blogjava.net/rongxh7谢谢! (*^-^*)
本文原创,转载请注明出处,谢谢!http://www.blogjava.net/rongxh7(心梦帆影JavaEE技术博客)