基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
来源:互联网 发布:arpscan linux下载 编辑:程序博客网 时间:2024/05/10 19:10
基于Annotation的SSH整合开发,其实,并没有我当初想像中那么顺利。真正去做的时候,才发觉有许多问题。但不要紧,探索一下吧。在探索过程中学到知识,才是最重要的。
言归正传,现在,我们加入Spring的支持:把spring-framework-2.5.5\dist中的spirng.jar引进我们项目的lib目录来,还要添加\lib\aspectj\下的两个jar包,以支持切面编程。
必要的配置文件还是要的:
applicationContext-common.xml
<beansxmlns="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"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="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: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-scanbase-package="com.rong"/>
<!--
<bean id="userDao" class="com.rong.dao.UserDaoBean">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userService" class="com.rong.service.UserServiceBean">
<property name="userDao" ref="userDao"/>
</bean>
-->
</beans>
关键的两点:
<context:annotation-config/>
<!-- 让Spring通过自动扫描来查询和管理Bean-->
<context:component-scanbase-package="com.rong"/>
这样配置之后,就省去了上面注释掉的DAO层和Service层等配置代码。是不是很方便呢。
关于这一部分的XML代码,我们下面还会作解释。
来开发我们的DAO层吧,接口如下:
import java.util.List;
import com.rong.entity.User;
publicinterface 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层的实现类:
import java.util.List;
import org.springframework.stereotype.Repository;
import com.rong.entity.User;
@Repository("userDao") //声明此类为数据持久层的类
publicclass UserDaoBeanextends MyHibernateDaoSupportimplements 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);
}
}
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
publicclass MyHibernateDaoSupportextends HibernateDaoSupport{
@Resource(name="sessionFactory") //为父类HibernateDaoSupport注入sessionFactory的值
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
}
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
到现在,我们再回过头来看applicationContext-common.xml中的
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<mapping resource="com/rong/entity/User.hbm.xml"/>
-->
<!-- 在Hibernate中注册User实体类,区别于上面注释掉的resource写法-->
<mapping class="com.rong.entity.User"/>
我们再来看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") //声明此类为业务逻辑层的类
publicclass UserServiceBeanimplements UserService{
@Autowired
private UserDao userDao;
public void save(User user){
userDao.save(user);
}
}
好,就开发到这样,是不是忘记了什么?记得要配置web.xml,部分代码如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<!-- 配置Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
是不是真的成功了?用Junit测试一下吧,我测试过是没问题的,由于篇幅,Junit的测试代码就不贴出来了。自己练习一下吧!
其实,到现在为止,我们发觉我们的XML配置文件还是很多。其实,这样想想,上一阶段我们省去了xxx.hbm.xml这类的文件,这一阶段,我们少去了<bean id="" class=""><property name="" ref="">这样的配置项。而这些,正是我们项目开发中,大量使用的配置。而只要书写简单的Annotation注解,就可以省去这样,我们何乐而不用。而那些我们保留的XML配置文件(如:数据库连接,事务),这样是写死的,一个项目就写一次或复制过来用,我们保留它又何妨?
好,暂时到这里,我们还有下一阶段的基于Annotation的SSH整合开发,我们将会以一个用户注册的例子,把Struts2的注解带到我们的整合开发中来。一起期待吧!
原文: http://www.blogjava.net/rongxh7
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 2
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
- 润乾报表超链接的几种方法
- 自己总结的常用的Oracle操作命令
- 重新解读HoG 和PHoG (pyramid HoG)
- 【Linux Shell】shell命令读书笔记---第三章:变量
- magento 开发笔记6
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
- ffmpeg tutorial 7:快进快退
- 究竟wifi是怎么定位我的?迷局与真相,阴谋与阳谋
- 腾讯2012实习生招聘技术类笔试题
- 分发APK文件时候,把APK传到网站上下载出现不能下载的问题
- 减小Delphi 2010/delphi XE编译出来的文件大小
- Win7 调整字体大小
- 在代码中创建 SuperToolTips
- Android设备双系统启动的原理