转载自: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,我灰常喜欢,因为我找不到更好更适合我的)
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,我想下面注释得也比较清楚了,如果我写错了或理解错了希望指正。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用 annotation --> <context:annotation-config /> <!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 --> <context:component-scan base-package="org.chinasb.framework.modules" /> <!-- hibernate属性配置 --> <context:property-placeholder location="classpath:resources/hibernate/hibernate.properties"/> <!-- Hibernate 配置管理 --> <import resource="applicationContext-persistence.xml" /> </beans>4.hiberante配置所需的一些属性,指定方言,开始hibernate缓存等,后面还有一个c3p0的数据连接池属性。你们下载的代码里面,数据源方面我换成了c3p0,因为proxool我配置的是随web启动的,而我又不想改成随spring加载启动。所以我开发中注释掉proxool,以后上线再打开。
## hibernate hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.format_sql=false hibernate.hbm2ddl.auto=update ## hibernate cache hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.cache.use_query_cache=false hibernate.cache.use_second_level_cache=true ## C3P0 configuration hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost:3306/chinasb??useUnicode=true&characterEncoding=utf-8 hibernate.connection.username=root hibernate.connection.password=root5.applicationContext-persistence.xml,这里就是数据源及事务的一些配置了。事务我使用了cglib类级别的代理注解配置方式,如果你喜欢用jdk接口方式动态的代理,可以去掉 proxy-target-class="true"。顺便也保留了声名式事务。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Proxool 数据源 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.default</value> </property> </bean> --> <!-- C3P0 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${hibernate.connection.driver_class}"/> <property name="jdbcUrl" value="${hibernate.connection.url}"/> <property name="properties"> <props> <prop key="user">${hibernate.connection.username}</prop> <prop key="password">${hibernate.connection.password}</prop> </props> </property> </bean> <!-- SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="org.chinasb.framework.modules.*.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> </props> </property> </bean> <!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置注解实现管理事务(cglib:proxy-target-class="true") --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 指定使用cglib --> <!-- <aop:aspectj-autoproxy proxy-target-class="true" /> --> <!-- 配置事务的传播特性 --> <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="batchUpdate" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> --> <!-- 配置事务的切入点 --> <!-- <aop:config> <aop:pointcut id="targetMethod" expression="execution(* org.chinasb.framework.modules..service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" /> </aop:config> --> </beans>6.struts.xml,你懂的。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 开启使用开发模式,详细错误提示 --> <constant name="struts.devMode" value="false" /> <!-- 将对象交给spring管理 --> <constant name="struts.objectFactory" value="spring" /> <!-- 指定资源编码类型 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 指定每次请求到达,重新加载资源文件 --> <constant name="struts.i18n.reload" value="false" /> <!-- 指定每次配置文件更改后,自动重新加载 --> <constant name="struts.configuration.xml.reload" value="false" /> <!-- 国际化资源文件 --> <constant name="struts.custom.i18n.resources" value="resources/content/Language" /> <!-- 默认后缀名 --> <constant name="struts.action.extension" value="do,action,jhtml,," /> <!-- Struts Annotation --> <constant name="actionPackages" value="org.chinasb.framework.modules"/> </struts>