步骤 1 : 重构完成之后的效果 步骤 2 : 重申Service层存在的重构需求 步骤 3 : Service层 的重构行为 步骤 4 : 父接口 BaseService 步骤 5 : 父接口实现类 BaseServiceImpl 步骤 6 : CategoryService接口继承BaseService 步骤 7 : CategoryServiceImpl继承BaseServiceImpl 步骤 8 : Clazz对象的处理 步骤 9 : CategoryAction的调整 步骤 10 : Service重构-抽象带来的好处 步骤 11 : 可运行项目
如图所示,Service层完成重构之后,CategoryService只需要继承BaseService,而CategoryServiceImpl也是只需要继承 BaseServiceImpl并实现CategoryService接口。
无需自己再写额外的代码,后续其他实体类的开发也能享受到这个好处,这样就大大地加快了开发的效率,减少了出错与维护成本。
在开发分类管理的过程中,Service层用到了一个接口和一个实现类,分别是CategoryService和CategoryServiceImpl。
这个层有什么问题呢? 首先看接口:CategoryService。 其声明的方法基本上就是CURD和分页。 可以预见的是,在后续做产品管理,用户管理,订单管理等等,也会有这么一个非常近似的CURD的接口,换句话说,这里是有做抽象和代码重构(Refactory)的机会和价值的。 然后看实现类:CategoryServiceImpl。 CategoryServiceImpl本身其实就是个架子,真正起作用的是为其注入的DAO对象,所以这个地方也是可以引入委派模式,使得代码调用更加顺畅。 package com.how2java.tmall.service;
import java.util.List;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.util.Page;
public interface CategoryService{
public List list();
public void save(Category category);
public int total();
public List<Category> listByPage(Page page);
public void delete(Category category);
public Category get(Class clazz, int id);
public void update(Category category);
}
package com.how2java.tmall.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.how2java.tmall.dao.impl.DAOImpl;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.service.CategoryService;
import com.how2java.tmall.util.Page;
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired DAOImpl dao;
@Override
public List list() {
DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
dc.addOrder(Order.desc("id"));
return dao.findByCriteria(dc);
}
@Override
public int total() {
String hql = "select count(*) from Category ";
List<Long> l= dao.find(hql);
if(l.isEmpty())
return 0;
Long result= l.get(0);
return result.intValue();
}
@Override
public List<Category> listByPage(Page page) {
DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
dc.addOrder(Order.desc("id"));
return dao.findByCriteria(dc,page.getStart(),page.getCount());
}
@Override
public void save(Category category) {
dao.save(category);
}
@Override
public void delete(Category category) {
dao.delete(category);
}
@Override
public Category get(Class clazz, int id) {
return (Category) dao.get(clazz, id);
}
@Override
public void update(Category category) {
dao.update(category);
}
}
由于可以预见的在后续做产品管理,用户管理,订单管理等等,也会有这么一个非常近似的CURD的接口,那么我们就做一个BaseService,里面就提供这些CRUD和分页查询的方法
注:save方法为什么不是返回void,而是要返回一个Integer。 这个问题在后续步骤有一个专门讲解:关于save方法的返回类型 注:提供了一个新的get(int id)方法,不需要指定clazz也行,只需要提供id即可。 package com.how2java.tmall.service;
import java.util.List;
import com.how2java.tmall.util.Page;
public interface BaseService {
public Integer save(Object object);
public void update(Object object);
public void delete(Object object);
public Object get(Class clazz,int id);
public Object get(int id);
public List list();
public List listByPage(Page page);
public int total();
}
package com.how2java.tmall.service; import java.util.List; import com.how2java.tmall.util.Page; public interface BaseService { public Integer save(Object object); public void update(Object object); public void delete(Object object); public Object get(Class clazz,int id); public Object get(int id); public List list(); public List listByPage(Page page); public int total(); }
接着,提供了一个BaseServiceImpl类来实现BaseService这个接口
package com.how2java.tmall.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.how2java.tmall.dao.impl.DAOImpl;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.service.BaseService;
import com.how2java.tmall.util.Page;
@Service
public class BaseServiceImpl implements BaseService {
@Autowired DAOImpl dao;
protected Class clazz =Category.class;
@Override
public List list() {
DetachedCriteria dc = DetachedCriteria.forClass(clazz);
dc.addOrder(Order.desc("id"));
return dao.findByCriteria(dc);
}
@Override
public int total() {
String hql = "select count(*) from " + clazz.getName() ;
List<Long> l= dao.find(hql);
if(l.isEmpty())
return 0;
Long result= l.get(0);
return result.intValue();
}
@Override
public List<Object> listByPage(Page page) {
DetachedCriteria dc = DetachedCriteria.forClass(clazz);
dc.addOrder(Order.desc("id"));
return dao.findByCriteria(dc,page.getStart(),page.getCount());
}
@Override
public Integer save(Object object) {
return (Integer) dao.save(object);
}
@Override
public void delete(Object object) {
dao.delete(object);
}
@Override
public Object get(Class clazz, int id) {
return dao.get(clazz, id);
}
@Override
public void update(Object object) {
dao.update(object);
}
@Override
public Object get(int id) {
return dao.get(clazz, id);
}
}
这样CategoryService就不需要自己声明方法了,只需要继承接口BaseService即可
package com.how2java.tmall.service;
public interface CategoryService extends BaseService{
}
package com.how2java.tmall.service; public interface CategoryService extends BaseService{ }
CategoryServiceImpl也不需要自己提供实现了,继承BaseServiceImpl 并实现接口CategoryService 即可
package com.how2java.tmall.service.impl;
import org.springframework.stereotype.Service;
import com.how2java.tmall.service.CategoryService;
@Service
public class CategoryServiceImpl extends BaseServiceImpl implements CategoryService {
}
package com.how2java.tmall.service.impl; import org.springframework.stereotype.Service; import com.how2java.tmall.service.CategoryService; @Service public class CategoryServiceImpl extends BaseServiceImpl implements CategoryService { }
可是现在有一个问题,BaseServiceImpl类里面有一行
Class clazz =Category.class; 表示这个BaseServiceImpl是专门用于进行Category类的CURD的,如果是后续要做的产品功能对应的ProductService继承BaseServiceImpl 这个类,不就有问题了吗? 这个问题怎么解决呢? 这个。。。这个。。。。处理起来比较复杂,分以下几个步骤进行 1. 声明clazz的时候,不再指向Category.class 对象 protected Class clazz; 2. 在构造方法中,借助异常处理和反射得到Category.class或者Product.class。 即要做到哪个类继承了BaseServiceImpl,clazz 就对应哪个类对象。 比如是 CategoryServiceImpl继承了BaseServiceImpl,那么这个clazz的值就是Category.class 比如是 ProductServiceImpl继承了BaseServiceImpl,那么这个clazz的值就是Product.class public BaseServiceImpl(){ try{ throw new Exception(); } catch(Exception e){ StackTraceElement stes[]= e.getStackTrace(); String serviceImpleClassName= stes[1].getClassName(); try { Class serviceImplClazz= Class.forName(serviceImpleClassName); String serviceImpleClassSimpleName = serviceImplClazz.getSimpleName(); String pojoSimpleName = serviceImpleClassSimpleName.replaceAll("ServiceImpl", ""); String pojoPackageName = serviceImplClazz.getPackage().getName().replaceAll(".service.impl", ".pojo"); String pojoFullName = pojoPackageName +"."+ pojoSimpleName; clazz=Class.forName(pojoFullName); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } } } 2.1 首先要获取是哪个类继承了BaseServiceImpl,这里用到了面向对象知识里的:实例化子类,父类的构造方法一定会被调用 这么一个知识点: try{ throw new Exception(); } catch(Exception e){ StackTraceElement stes[]= e.getStackTrace(); String serviceImpleClassName= stes[1].getClassName(); 所以在父类BaseServiceImpl里故意抛出一个异常,然后手动捕捉住它,在其对应的StackTrace里的第二个(下标是1) 栈跟踪元素StackTraceElement ,即对应子类。 这样我们就拿到了子类名称 CategoryServiceImpl或者ProductServiceImpl 2.2 拿到了CategoryServiceImpl或者ProductServiceImpl,通过字符串替换,拼接和反射,就得到了对应的实体类的类对象Category.class或者Product.class对象。 Class serviceImplClazz= Class.forName(serviceImpleClassName); String serviceImpleClassSimpleName = serviceImplClazz.getSimpleName(); String pojoSimpleName = serviceImpleClassSimpleName.replaceAll("ServiceImpl", ""); String pojoPackageName = serviceImplClazz.getPackage().getName().replaceAll(".service.impl", ".pojo"); String pojoFullName = pojoPackageName +"."+ pojoSimpleName; clazz=Class.forName(pojoFullName); 这里需要注意的是,这样的做法是建立在服务实现类是放在xxx.service.impl包下的,而实体类是放在xxx.pojo包下的。 3. 同时提供了一个测试方法,运行一下,看看这个clazz引用是否指向了期望的类对象。 public static void main(String[] args) { new CategoryServiceImpl().showClass(); } public void showClass(){ System.out.println(clazz); } 参考知识: 反射,异常处理 package com.how2java.tmall.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.how2java.tmall.dao.impl.DAOImpl;
import com.how2java.tmall.service.BaseService;
import com.how2java.tmall.util.Page;
@Service
public class BaseServiceImpl implements BaseService {
@Autowired DAOImpl dao;
protected Class clazz;
public static void main(String[] args) {
new CategoryServiceImpl().showClass();
}
public void showClass(){
System.out.println(clazz);
}
public BaseServiceImpl(){
try{
throw new Exception();
}
catch(Exception e){
StackTraceElement stes[]= e.getStackTrace();
String serviceImpleClassName= stes[1].getClassName();
try {
Class serviceImplClazz= Class.forName(serviceImpleClassName);
String serviceImpleClassSimpleName = serviceImplClazz.getSimpleName();
String pojoSimpleName = serviceImpleClassSimpleName.replaceAll("ServiceImpl", "");
String pojoPackageName = serviceImplClazz.getPackage().getName().replaceAll(".service.impl", ".pojo");
String pojoFullName = pojoPackageName +"."+ pojoSimpleName;
clazz=Class.forName(pojoFullName);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
}
@Override
public List list() {
DetachedCriteria dc = DetachedCriteria.forClass(clazz);
dc.addOrder(Order.desc("id"));
return dao.findByCriteria(dc);
}
@Override
public int total() {
String hql = "select count(*) from " + clazz.getName() ;
List<Long> l= dao.find(hql);
if(l.isEmpty())
return 0;
Long result= l.get(0);
return result.intValue();
}
@Override
public List<Object> listByPage(Page page) {
DetachedCriteria dc = DetachedCriteria.forClass(clazz);
dc.addOrder(Order.desc("id"));
return dao.findByCriteria(dc,page.getStart(),page.getCount());
}
@Override
public Integer save(Object object) {
return (Integer) dao.save(object);
}
@Override
public void delete(Object object) {
dao.delete(object);
}
@Override
public Object get(Class clazz, int id) {
return dao.get(clazz, id);
}
@Override
public void update(Object object) {
dao.update(object);
}
}
因为BaseService接口中方法声明的改变,所以CategoryAction在调用的时候,需要做相应的调整。
这里主要是categoryService.get方法,由原本需要制定class对象,变为只需要给出id即可。 //category = categoryService.get(Category.class,id); category = (Category) categoryService.get(id); package com.how2java.tmall.action;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.service.CategoryService;
import com.how2java.tmall.util.ImageUtil;
import com.how2java.tmall.util.Page;
@Namespace("/")
@ParentPackage("basicstruts")
@Results(
{
/*分类管理*/
@Result(name="listCategory", location="/admin/listCategory.jsp"),
@Result(name="listCategoryPage", type = "redirect", location="/admin_category_list"),
@Result(name="editCategory", location="/admin/editCategory.jsp"),
})
public class CategoryAction {
@Autowired
CategoryService categoryService;
List<Category> categorys;
Category category;
File img;
Page page;
@Action("admin_category_list")
public String list() {
if(page==null)
page = new Page();
int total = categoryService.total();
page.setTotal(total);
categorys = categoryService.listByPage(page);
return "listCategory";
}
@Action("admin_category_add")
public String add() {
categoryService.save(category);
File imageFolder= new File(ServletActionContext.getServletContext().getRealPath("img/category"));
File file = new File(imageFolder,category.getId()+".jpg");
try {
FileUtils.copyFile(img, file);
BufferedImage img = ImageUtil.change2jpg(file);
ImageIO.write(img, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
return "listCategoryPage";
}
@Action("admin_category_delete")
public String delete() {
categoryService.delete(category);
return "listCategoryPage";
}
@Action("admin_category_edit")
public String edit() {
int id = category.getId();
// category = categoryService.get(Category.class,id);
category = (Category) categoryService.get(id);
return "editCategory";
}
@Action("admin_category_update")
public String update() {
categoryService.update(category);
if(null!=img){
File imageFolder= new File(ServletActionContext.getServletContext().getRealPath("img/category"));
File file = new File(imageFolder,category.getId()+".jpg");
try {
FileUtils.copyFile(img, file);
BufferedImage img = ImageUtil.change2jpg(file);
ImageIO.write(img, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
}
return "listCategoryPage";
}
public List<Category> getCategorys() {
return categorys;
}
public void setCategorys(List<Category> categorys) {
this.categorys = categorys;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public File getImg() {
return img;
}
public void setImg(File img) {
this.img = img;
}
}
这么做的好处主要在于:后续新功能开发的过程中,当需要新增加新的Service类的话,比如PropertyService,无需从头开发,只需要继承BaseServiceImpl 并实现PropertyService,那么其所需要CRUD一套方法都有了。
1. 开发成本显著降低 2. 更加不容易出错(因为方法都被抽象在父类中了,并且被前面的业务验证过了,要出错早就被纠正过了) package com.how2java.tmall.service.impl;
import org.springframework.stereotype.Service;
import com.how2java.tmall.service.PropertyService;
@Service
public class PropertyServiceImpl extends BaseServiceImpl implements PropertyService {
}
package com.how2java.tmall.service.impl; import org.springframework.stereotype.Service; import com.how2java.tmall.service.PropertyService; @Service public class PropertyServiceImpl extends BaseServiceImpl implements PropertyService { }
重构不会影响功能性,所以重构之后的代码一样可以实现分类的管理功能。
鉴于重构经历了较多的步骤,并且略微复杂,任何一步出了问题都会导致目前项目运行失败,在右上角有本知识点对应的可运行项目下载 ,实在自己搞不出来,就下载解压出来比较一下。
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
![]() |