【第十二章】零配置 之 12.5 综合示例-积分商城 ——跟我学spring3

12.5  综合示例

12.5.1  概述

在第十一章中我们介绍了SSH集成,在进行SSH集成时都是通过基于XML配置文件配置每层的Bean,从而产生许多XML配置文件,本节将通过注解方式消除部分XML配置文件,实现所谓的零配置。

 

12.5.2  项目拷贝

       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项目。

 

12.5.3  数据访问层变化

       将dao层配置文件中的dao实现Bean定义删除,通过在dao实现类头上添加“@Repository”来定义dao实现Bean,并通过注解@Autowired来完成依赖注入。

 

1、删除DAO层配置文件(cn/javass/point/dao/applicationContext-hibernate.xml)中的如下配置:

 

java代码:
Java代码   收藏代码
  1. <bean id="abstractDao" abstract="true" init-method="init">  
  2. <property name="sessionFactory" ref="sessionFactory"/>  
  3. </bean>     
  4. <bean id="goodsDao" class="cn.javass.point.dao.hibernate.GoodsHibernateDao"  
  5. parent="abstractDao"/>  
  6. <bean id="goodsCodeDao" class="cn.javass.point.dao.hibernate.GoodsCodeHibernateDao"          
  7. parent="abstractDao"/>  

 

 

2、修改通用DAO实现cn.javass.commons.dao.hibernate.BaseHibernateDao,通过注解实现依赖注入和指定初始化方法:

 

java代码:
Java代码   收藏代码
  1. public abstract class BaseHibernateDao<M extends Serializable, PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<M, PK>, InitializingBean {  
  2.     //省略类字段  
  3.     @Autowired @Required  
  4.     public void setSf(SessionFactory sf) {  
  5.         setSessionFactory(sf);  
  6.     }  
  7.     @PostConstruct  
  8.     @SuppressWarnings("unchecked")  
  9.     public void init() {  
  10.         //省略具体实现代码  
  11.     }  
  12. }  
  • setSf方法:通过@Autowired注解自动注入SessionFactory实现;
  • init方法:通过@PostConstruct注解表示该方法是初始化方法;

 

3、修改cn.javass.point.dao.hibernate.GoodsHibernateDao,在该类上添加@Repository注解来进行DAO层Bean定义:

 

java代码:
Java代码   收藏代码
  1. @Repository  
  2. public class GoodsHibernateDao extends BaseHibernateDao<GoodsModel, Integer> implements IGoodsDao {  
  3. ……  
  4. }  

 

4、修改cn.javass.point.dao.hibernate.GoodsCodeHibernateDao,在该类上添加@Repository注解来进行DAO层Bean定义:

 

java代码:
Java代码   收藏代码
  1. @Repository  
  2. public class GoodsCodeHibernateDao extends BaseHibernateDao<GoodsCodeModel, Integer> implements IGoodsCodeDao {  
  3. ……  
  4. }  

DAO层到此就修改完毕,其他地方无需修改。

 

12.5.4  业务逻辑层变化

  将service层配置文件中的service实现Bean定义删除,通过在service实现类头上添加“@Service”来定义service实现Bean,并通过注解@Autowired来完成依赖注入。

 

1、删除Service层配置文件(cn/javass/point/service/applicationContext-service.xml)中的如下配置:

 

java代码:
Java代码   收藏代码
  1. <bean id="goodsService" class="cn.javass.point.service.impl.GoodsServiceImpl">  
  2.     <property name="dao" ref="goodsDao"/>  
  3. </bean>  
  4. <bean id="goodsCodeService" class="cn.javass.point.service.impl.GoodsCodeServiceImpl">  
  5.     <property name="dao" ref="goodsCodeDao"/>  
  6.     <property name="goodsService" ref="goodsService"/>  
  7. </bean>  

 

 

2、修改cn.javass.point.service.impl.GoodsServiceImpl,在该类上添加@Service注解来进行Service层Bean定义:

 

java代码:
Java代码   收藏代码
  1. @Service  
  2. public class GoodsServiceImpl extends BaseServiceImpl<GoodsModel, Integer> implements IGoodsService {  
  3.      
  4.     @Autowired @Required  
  5.     public void setGoodsDao(IGoodsDao dao) {  
  6.         setDao(dao);  
  7.     }  
  8. }  
  • setGoodsDao方法:用于注入IGoodsDao实现,此处直接委托给setDao方法。

 

3、修改cn.javass.point.service.impl.GoodsCodeServiceImpl,在该类上添加@Service注解来进行Service层Bean定义:

 

java代码:
Java代码   收藏代码
  1. @Service  
  2. public class GoodsCodeServiceImpl extends BaseServiceImpl<GoodsCodeModel, Integer> implements IGoodsCodeService {  
  3.     @Autowired @Required  
  4.     public void setGoodsCodeDao(IGoodsCodeDao dao) {  
  5.         setDao(dao);  
  6.     }  
  7.     @Autowired @Required  
  8.     public void setGoodsService(IGoodsService goodsService) {  
  9.         this.goodsService = goodsService;  
  10.     }  
  11. }  
  • setGoodsCodeDao方法:用于注入IGoodsCodeDao实现,此处直接委托给setDao方法;
  • setGoodsService方法:用于注入IGoodsService实现。

 Service层到此就修改完毕,其他地方无需修改。

 

12.5.5  表现层变化

类似于数据访问层和业务逻辑层修改,对于表现层配置文件直接删除,通过在action实现类头上添加“@Controller”来定义action实现Bean,并通过注解@Autowired来完成依赖注入。

 

1、  删除表现层所有Spring配置文件(cn/javass/point/web):

 

java代码:
Java代码   收藏代码
  1. cn/javass/point/web/pointShop-admin-servlet.xml  
  2. cn/javass/point/web/pointShop-front-servlet.xml  

 

 

2、修改表现层管理模块的cn.javass.point.web.admin.action.GoodsAction,在该类上添加@Controller注解来进行表现层Bean定义,且作用域为“prototype”:

 

java代码:
Java代码   收藏代码
  1. @Controller("/admin/goodsAction")  
  2. @Scope("prototype")  
  3. public class GoodsAction extends BaseAction {  
  4.     private IGoodsService goodsService;  
  5.     @Autowired @Required  
  6.     public void setGoodsService(IGoodsService goodsService) {  
  7.         this.goodsService = goodsService;  
  8.     }  
  9. }  
  • setGoodsService方法:用于注入IGoodsService实现。

 

3、修改表现层管理模块的cn.javass.point.web.admin.action.GoodsCodeAction,在该类上添加@Controller注解来进行表现层Bean定义,且作用域为“prototype”:

 

java代码:
Java代码   收藏代码
  1. @Controller("/admin/goodsCodeAction")  
  2. @Scope("prototype")  
  3. public class GoodsCodeAction extends BaseAction {  
  4.     @Autowired @Required  
  5.     public void setGoodsCodeService(IGoodsCodeService goodsCodeService) {  
  6.         this.goodsCodeService = goodsCodeService;  
  7.     }  
  8.     @Autowired @Required  
  9.     public void setGoodsService(IGoodsService goodsService) {  
  10.         this.goodsService = goodsService;  
  11.     }  
  12. }  
  • setGoodsCodeService方法:用于注入IGoodsCodeService实现;
  • setGoodsService方法:用于注入IGoodsService实现。

 

3、修改表现层前台模块的cn.javass.point.web.front.action.GoodsAction,在该类上添加@Controller注解来进行表现层Bean定义,且作用域为“prototype”:

 

java代码:
Java代码   收藏代码
  1. @Controller("/front/goodsAction")  
  2. @Scope("prototype")  
  3. public class GoodsAction extends BaseAction {  
  4.     @Autowired @Required  
  5.     public void setGoodsService(IGoodsService goodsService) {  
  6.         this.goodsService = goodsService;  
  7.     }  
  8.     @Autowired @Required  
  9.     public void setGoodsCodeService(IGoodsCodeService goodsCodeService) {  
  10.         this.goodsCodeService = goodsCodeService;  
  11.     }  
  12. }  
  • setGoodsCodeService方法:用于注入IGoodsCodeService实现;
  • setGoodsService方法:用于注入IGoodsService实现。

 

12.5.6  其他变化

1、定义一个基于Java方法的配置类,用于加载XML配置文件:

 

java代码:
Java代码   收藏代码
  1. package cn.javass.point;  
  2. import org.springframework.context.annotation.Configuration;  
  3. import org.springframework.context.annotation.ImportResource;  
  4. @Configuration  
  5. @ImportResource(  
  6.         {"classpath:applicationContext-resources.xml",  
  7.          "classpath:cn/javass/point/dao/applicationContext-hibernate.xml",  
  8.          "classpath:cn/javass/point/service/applicationContext-service.xml"  
  9.         })  
  10. public class AppConfig {  
  11. }  

    该类用于加载零配置中一般不变的XML配置文件,如事务管理,数据源、SessionFactory,这些在几乎所有项目中都是类似的,因此可以作为通用配置。

 

2、修改集成其它Web框架的通用配置,将如下配置:

 

java代码:
Java代码   收藏代码
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.         classpath:applicationContext-resources.xml,  
  5.         classpath:cn/javass/point/dao/applicationContext-hibernate.xml,  
  6.         classpath:cn/javass/point/service/applicationContext-service.xml,  
  7.         classpath:cn/javass/point/web/pointShop-admin-servlet.xml,  
  8.         classpath:cn/javass/point/web/pointShop-front-servlet.xml  
  9.     </param-value>  
  10. </context-param>  

 

修改为如下配置:

 

 

java代码:
Java代码   收藏代码
  1. <context-param>  
  2.   <param-name>contextClass</param-name>  
  3.   <param-value>  
  4.        org.springframework.web.context.support.AnnotationConfigWebApplicationContext      
  5.   </param-value>  
  6. </context-param>  
  7. <context-param>  
  8.   <param-name>contextConfigLocation</param-name>  
  9.   <param-value>cn.javass.point</param-value>  
  10. </context-param>  
  • contextClass:使用notationConfigWebApplicationContext替换默认的XmlWebApplicationContext;
  • contextConfigLocation:指定为“cn.javass.point”,表示将通过扫描该类路径“cn.javass.point”下的注解类来进行加载Bean定义。

 

启动pointShop2项目,在浏览器输入http://localhost:8080/pointShop2/admin/goods/list.action访问积分商城后台,如果没问题说明零配置整合成功。

 

到此零配置方式实现SSH集成已经整合完毕,相对于基于XML方式主要减少了配置的数量和配置文件的数量。

 

 

原创内容,转载请注明私塾在线【http://sishuok.com/forum/blogPost/list/2553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值