http://www.oschina.net/code/snippet_147955_5079
这是我为新项目专门搭建的基于全注解方式的SSH基础框架,虽然是老掉牙的的东西,自我感觉很良好,好东西不敢独享,晒晒等拍砖。
概述:基于struts2.23 + spring2.5.6 + hibernate3.6.4 + hibernate-generic-dao1.0(除了spring,我整合的都是最新的GA包,hibernate-generic-dao是google项目库中一个开源的basedao,我灰常喜欢,因为我找不到更好更适合我的)
项目代码是基于eclipse3.6创建的,很简单,大家直接导入则可运行。
1.包结构,源码,测试用例,配置文件一目了然。每个功能模块都在modules包下做开发,配置文件统一在resource管理(基实也没多少配置文件,都用注解嘛)。
2.无论阅读哪个web项目代码,我都是先从web.xml开始,项目有什么东西一清二楚。
我这里将log4j监听放在第一,我想他应该能够从系统启动开启就能记录我的所有日志(求认证)。第二个监听是proxool数据库连接池,听说很高效,所以果断引入(引入步骤搞得复杂吧,我还重写了监听。一切为了稳定,也好扩展我某日喜欢加入动态切换数据源做准备。呵呵)。OpenSessionInView,我想如果你不喜欢可以摘掉,反正我很喜欢。Struts2指定了自定义的struts.xml文件位置,指定扫描注解的action路径。最后是proxool的可视化图形监控,很棒。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>framework</display-name> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/resources/log4j/log4j.properties</param-value> </context-param> <context-param> <param-name>propertyFile</param-name> <param-value>/WEB-INF/classes/resources/hibernate/proxool.properties</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:resources/spring/applicationContext.xml</param-value> </context-param> <!-- log4j Listener --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- Proxool Listener --> <listener> <listener-class>org.chinasb.framework.core.db.ProxoolListener</listener-class> </listener> <!-- Open Session In View Filter --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring Listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Character Encoding Filter --> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <!-- 强制进行转码 --> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 延长action中属性的生命周期, --> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Struts2 Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,resources/struts/struts.xml</param-value> </init-param> <init-param> <param-name>actionPackages</param-name> <param-value>org.chinasb.framework.modules</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Proxool Monitoring --> <servlet> <servlet-name>Admin</servlet-name> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Admin</servlet-name> <url-pattern>/admin.html</url-pattern> </servlet-mapping> <!-- Welcome List --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 3.applicationContext.xml,我想下面注释得也比较清楚了,如果我写错了或理解错了希望指正。好了,下面我简单讲一下开发流程。
在modules下建立模块,和相应的包(action,dao,model,service,util),比如我上面包结构的demo模块。
demo.java,model类,映射数据库中的表,每个model一张表,为了适应basedao,每个model还对应每个dao(不要觉得这是麻烦的)。jpa的注解,你们懂的,不解释。
package org.chinasb.framework.modules.demo.model; import java.io.Serializable; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class Demo implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String title; private String content; private Date publishdate; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getPublishdate() { return publishdate; } public void setPublishdate(Date publishdate) { this.publishdate = publishdate; } } DemoDao,接口类。我想在多数程序员的项目里做的绝大多数事情都是相关数据库的CURD操作,所以基础框架里整合一个强大的BaseDao是必须的,而我选择了开源的GenericDao(觉得不适合你的可以替掉)。直接继承GenericDao接口,指定操作的model,与主键的类型。最后就是测试用例了,这里使用了经典的junit4.4版本,依然是注解方式。
BaseTestTemplate,大家都不想每个用例都去写一次读取applicationContext吧,把一些公用的东西封装起来吧。
package org.chinasb.framework.core.base.test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:resources/spring/applicationContext.xml" }) @TransactionConfiguration(defaultRollback = false) @Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public class BaseTestTemplate { } DemoActionTest,简单的测试用例,这里只是为了说明如何在这个框架里进行单元测试。所以我的目的达到了,简单吧。
测试表:
众望所归,出图:
终于写完了,好累啊。下一步跟汪兄商量如何完美整合他那个强大的数据级权限中间件(ralasafe),这样在未来一投入使用即附带有权限控制,多爽。好了,大家看得也累,喜欢的,不喜欢的都出来拍拍砖吧。不对的地方,请各位N人多多指正。
源码:http://www.chinasb.org/wp-content/uploads/2011/07/framework.zip
Google Code:http://code.google.com/p/ssh-base-framework/
Google Code Download:http://code.google.com/p/ssh-base-framework/downloads/list
Google Code SVN:http://ssh-base-framework.googlecode.com/svn/trunk/