在第十一章中我们介绍了SSH集成,在进行SSH集成时都是通过基于XML配置文件配置每层的Bean,从而产生许多XML配置文件,本节将通过注解方式消除部分XML配置文件,实现所谓的零配置。
1、拷贝【第十一章 SSH集成开发】中的“pointShop”项目将其命名为“pointShop2”;
2、修改“pointShop2”项目下的“.settings”文件夹下的“org.eclipse.wst.common.component”文件,将“<property name="context-root" value="pointShop"/>”修改为“<property name="context-root" value="pointShop2"/>”,即该web项目的上下文为“pointShop2”,在浏览器中可以通过http://localhost:8080/pointShop2来访问该web项目。
将dao层配置文件中的dao实现Bean定义删除,通过在dao实现类头上添加“@Repository”来定义dao实现Bean,并通过注解@Autowired来完成依赖注入。
1、删除DAO层配置文件(cn/javass/point/dao/applicationContext-hibernate.xml)中的如下配置:
<bean id="abstractDao" abstract="true" init-method="init"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="goodsDao" class="cn.javass.point.dao.hibernate.GoodsHibernateDao" parent="abstractDao"/> <bean id="goodsCodeDao" class="cn.javass.point.dao.hibernate.GoodsCodeHibernateDao" parent="abstractDao"/>
2、修改通用DAO实现cn.javass.commons.dao.hibernate.BaseHibernateDao,通过注解实现依赖注入和指定初始化方法:
public abstract class BaseHibernateDao<M extends Serializable, PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<M, PK>, InitializingBean { //省略类字段 @Autowired @Required public void setSf(SessionFactory sf) { setSessionFactory(sf); } @PostConstruct @SuppressWarnings("unchecked") public void init() { //省略具体实现代码 } }
3、修改cn.javass.point.dao.hibernate.GoodsHibernateDao,在该类上添加@Repository注解来进行DAO层Bean定义:
@Repository public class GoodsHibernateDao extends BaseHibernateDao<GoodsModel, Integer> implements IGoodsDao { …… }
4、修改cn.javass.point.dao.hibernate.GoodsCodeHibernateDao,在该类上添加@Repository注解来进行DAO层Bean定义:
@Repository public class GoodsCodeHibernateDao extends BaseHibernateDao<GoodsCodeModel, Integer> implements IGoodsCodeDao { …… }
DAO层到此就修改完毕,其他地方无需修改。
将service层配置文件中的service实现Bean定义删除,通过在service实现类头上添加“@Service”来定义service实现Bean,并通过注解@Autowired来完成依赖注入。
1、删除Service层配置文件(cn/javass/point/service/applicationContext-service.xml)中的如下配置:
<bean id="goodsService" class="cn.javass.point.service.impl.GoodsServiceImpl"> <property name="dao" ref="goodsDao"/> </bean> <bean id="goodsCodeService" class="cn.javass.point.service.impl.GoodsCodeServiceImpl"> <property name="dao" ref="goodsCodeDao"/> <property name="goodsService" ref="goodsService"/> </bean>
2、修改cn.javass.point.service.impl.GoodsServiceImpl,在该类上添加@Service注解来进行Service层Bean定义:
@Service public class GoodsServiceImpl extends BaseServiceImpl<GoodsModel, Integer> implements IGoodsService { @Autowired @Required public void setGoodsDao(IGoodsDao dao) { setDao(dao); } }
3、修改cn.javass.point.service.impl.GoodsCodeServiceImpl,在该类上添加@Service注解来进行Service层Bean定义:
@Service public class GoodsCodeServiceImpl extends BaseServiceImpl<GoodsCodeModel, Integer> implements IGoodsCodeService { @Autowired @Required public void setGoodsCodeDao(IGoodsCodeDao dao) { setDao(dao); } @Autowired @Required public void setGoodsService(IGoodsService goodsService) { this.goodsService = goodsService; } }
Service层到此就修改完毕,其他地方无需修改。
类似于数据访问层和业务逻辑层修改,对于表现层配置文件直接删除,通过在action实现类头上添加“@Controller”来定义action实现Bean,并通过注解@Autowired来完成依赖注入。
1、 删除表现层所有Spring配置文件(cn/javass/point/web):
cn/javass/point/web/pointShop-admin-servlet.xml cn/javass/point/web/pointShop-front-servlet.xml
2、修改表现层管理模块的cn.javass.point.web.admin.action.GoodsAction,在该类上添加@Controller注解来进行表现层Bean定义,且作用域为“prototype”:
@Controller("/admin/goodsAction") @Scope("prototype") public class GoodsAction extends BaseAction { private IGoodsService goodsService; @Autowired @Required public void setGoodsService(IGoodsService goodsService) { this.goodsService = goodsService; } }
3、修改表现层管理模块的cn.javass.point.web.admin.action.GoodsCodeAction,在该类上添加@Controller注解来进行表现层Bean定义,且作用域为“prototype”:
@Controller("/admin/goodsCodeAction") @Scope("prototype") public class GoodsCodeAction extends BaseAction { @Autowired @Required public void setGoodsCodeService(IGoodsCodeService goodsCodeService) { this.goodsCodeService = goodsCodeService; } @Autowired @Required public void setGoodsService(IGoodsService goodsService) { this.goodsService = goodsService; } }
3、修改表现层前台模块的cn.javass.point.web.front.action.GoodsAction,在该类上添加@Controller注解来进行表现层Bean定义,且作用域为“prototype”:
@Controller("/front/goodsAction") @Scope("prototype") public class GoodsAction extends BaseAction { @Autowired @Required public void setGoodsService(IGoodsService goodsService) { this.goodsService = goodsService; } @Autowired @Required public void setGoodsCodeService(IGoodsCodeService goodsCodeService) { this.goodsCodeService = goodsCodeService; } }
1、定义一个基于Java方法的配置类,用于加载XML配置文件:
package cn.javass.point; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource( {"classpath:applicationContext-resources.xml", "classpath:cn/javass/point/dao/applicationContext-hibernate.xml", "classpath:cn/javass/point/service/applicationContext-service.xml" }) public class AppConfig { }
该类用于加载零配置中一般不变的XML配置文件,如事务管理,数据源、SessionFactory,这些在几乎所有项目中都是类似的,因此可以作为通用配置。
2、修改集成其它Web框架的通用配置,将如下配置:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext-resources.xml, classpath:cn/javass/point/dao/applicationContext-hibernate.xml, classpath:cn/javass/point/service/applicationContext-service.xml, classpath:cn/javass/point/web/pointShop-admin-servlet.xml, classpath:cn/javass/point/web/pointShop-front-servlet.xml </param-value> </context-param>
修改为如下配置:
<context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>cn.javass.point</param-value> </context-param>
启动pointShop2项目,在浏览器输入http://localhost:8080/pointShop2/admin/goods/list.action访问积分商城后台,如果没问题说明零配置整合成功。
到此零配置方式实现SSH集成已经整合完毕,相对于基于XML方式主要减少了配置的数量和配置文件的数量。
原创内容,转载请注明私塾在线【http://sishuok.com/forum/blogPost/list/2553.html】