读书人

大伙帮忙看一下这样的SSH框架结构好不

发布时间: 2013-04-05 10:24:33 作者: rapoo

大家帮忙看一下这样的SSH框架结构好不好



说明:

红色部门是DAO

蓝色部门是SERVICE

绿色部门是ACTION(或是Spring的Controller)



DAO部分说明

BaseDao是基类,所有的DAO都继承此BaseDao,BaseDao继承了HibernateDaoSupport,用HibernateTemplate实现了一些基础的方法(插删改查),以下是代码
Java代码





对于所有模块的DAO,Spring配置中都注入到了DaoFactory中,DaoFactory代码如下
Java代码

package com.thd.dao;

import com.thd.dao.tree.TreeDao;
import com.thd.dao.user.UserDao;

public class DaoFactory {
private UserDao userDao;
private TreeDao treeDao;
public TreeDao getTreeDao() {
return treeDao;
}

public void setTreeDao(TreeDao treeDao) {
this.treeDao = treeDao;
}

public UserDao getUserDao() {
return userDao;
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

DaoFactory中有所有模块的DAO提供给Service使用。





SERVICE部门说明

BaseServiceImpl是所有模块Service的基类,BaseServiceImpl有DaoFactory的引用 还有ServiceFactory的引用(因为有可能一个Service调用另外一个Service的方法,所以引用了ServiceFactory,ServiceFactory在下面有说明),所有模块的Service继承BaseServiceImpl后就可以拿到所有的DAO和Service。ServiceFactory分为两种IocServiceFactoryImpl和InjectionServiceFactoryImpl,他们都实现了ServiceFactory接口(此接口有所有模块Service的Setter Getter方法),只不过Getter方式不同,一个是通过Spring IOC容器注入然后获取Service Bean,一个是通过代码拿到Spring IOC中的Service Bean.ServiceFactory的实现注入到BaseServiceImpl以提供给所有模块的Service。



IocServiceFactoryImpl的代码如下:
Java代码

package com.thd.serviceimpl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.thd.service.ServiceFactory;
import com.thd.service.tree.TreeService;
import com.thd.service.user.UserService;
/**
* @description 此类是通过从Spring ioc容器中直接获取service的封装


*
*/
public class IocServiceFactoryImpl implements ServiceFactory {

private ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appContext-*.xml");

public UserService getUserService(){
return (UserService)factory.getBean("userService");
};

public TreeService getTreeService(){
return (TreeService)factory.getBean("treeService");
};




}



InjectionServiceFactoryImpl的代码如下:
Java代码

package com.thd.serviceimpl;

import com.thd.service.ServiceFactory;
import com.thd.service.tree.TreeService;
import com.thd.service.user.UserService;
/**
* @description 此类是通过注入的方式来封装所有的service
*
*/
public class InjectionServiceFactoryImpl implements ServiceFactory{
private UserService userService;
private TreeService treeService;
public TreeService getTreeService() {
return treeService;
}
public void setTreeService(TreeService treeService) {
this.treeService = treeService;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}







Action部分说明

ServiceFactory被注入到PubAction ,所有模块的Action继承PubAction获取ServiceFactory来拿到所有的Service。





最后事务切的是Service层



这样配置后 所有模块的Action可以拿到所有模块的Service来进行操作,模块的Service可以拿到所有的DAO,以及可以做到Service之前的相互调用,我感觉很方便



附件中是源码和图片



大家评评这么配置的优点和缺点,有什么地方还有不足的还望指教
[解决办法]
框架还好 不用弄得那么整

读书人网 >Java Web开发

热点推荐