ssh的一点想法.docx
《ssh的一点想法.docx》由会员分享,可在线阅读,更多相关《ssh的一点想法.docx(25页珍藏版)》请在冰豆网上搜索。
ssh的一点想法
是我为新项目专门搭建的基于全注解方式的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的可视化图形监控,很棒。
xmlversion="1.0"encoding="UTF-8"?
>
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="xmlns:
web="
xsi:
schemaLocation="
id="WebApp_ID"version="2.5">
framework
log4jConfigLocation
/WEB-INF/classes/resources/log4j/log4j.properties
propertyFile
/WEB-INF/classes/resources/hibernate/proxool.properties
contextConfigLocation
classpath:
resources/spring/applicationContext.xml
--log4jListener-->
org.springframework.web.util.Log4jConfigListener
--ProxoolListener-->
org.chinasb.framework.core.db.ProxoolListener
--OpenSessionInViewFilter-->
OpenSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
singleSession
true
OpenSessionInViewFilter
/*
--SpringListener-->
org.springframework.web.context.ContextLoaderListener
--CharacterEncodingFilter-->
SetCharacterEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
--强制进行转码-->
true
SetCharacterEncoding
/*
--延长action中属性的生命周期,-->
struts-cleanup
org.apache.struts2.dispatcher.ActionContextCleanUp
struts-cleanup
/*
--Struts2Filter-->
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
config
struts-default.xml,struts-plugin.xml,resources/struts/struts.xml
actionPackages
org.chinasb.framework.modules
struts2
/*
--ProxoolMonitoring-->
Admin
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
Admin
/admin.html
--WelcomeList-->
index.html
index.htm
index.jsp
3.applicationContext.xml,我想下面注释得也比较清楚了,如果我写错了或理解错了希望指正。
xmlversion="1.0"encoding="UTF-8"?
>
//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-->
annotation-config/>
--使用annotation自动注册bean,并检查@Controller,@Service,@Repository注解已被注入-->
component-scanbase-package="org.chinasb.framework.modules"/>
--hibernate属性配置-->
property-placeholderlocation="classpath:
resources/hibernate/hibernate.properties"/>
--Hibernate配置管理-->
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
##hibernatecache
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=true
##C3P0configuration
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=root
5.applicationContext-persistence.xml,这里就是数据源及事务的一些配置了。
事务我使用了cglib类级别的代理注解配置方式,如果你喜欢用jdk接口方式动态的代理,可以去掉proxy-target-class="true"。
顺便也保留了声名式事务。
xmlversion="1.0"encoding="UTF-8"?
>
//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数据源-->
--
org.logicalcobwebs.proxool.ProxoolDriver
proxool.default
-->
--C3P0数据源-->
${hibernate.connection.username}
${hibernate.connection.password}
--SessionFactory-->
${hibernate.dialect}
${hibernate.show_sql}
${hibernate.format_sql}
${hibernate.hbm2ddl.auto}
${hibernate.cache.provider_class}
${hibernate.cache.use_query_cache}
${hibernate.cache.use_second_level_cache}
--配置事务管理-->
--配置注解实现管理事务(cglib:
proxy-target-class="true")-->
annotation-driventransaction-manager="transactionManager"proxy-target-class="true"/>
--指定使用cglib-->
--aspectj-autoproxyproxy-target-class="true"/>-->
--配置事务的传播特性-->
--
adviceid="txAdvice"transaction-manager="transactionManager">
attributes>
methodname="add*"propagation="REQUIRED"/>
methodname="edit*"propagation="REQUIRED"/>
methodname="remove*"propagation="REQUIRED"/>
methodname="save*"propagation="REQUIRED"/>
methodname="update*"propagation="REQUIRED"/>
methodname="delete*"propagation="REQUIRED"/>
methodname="batchUpdate"propagation="REQUIRED"/>
methodname="*"read-only="true"/>
attributes>
advice>
-->
--配置事务的切入点-->
--
config>
pointcutid="targetMethod"expression="execution(*org.chinasb.framework.modules..service.*.*(..))"/>
advisoradvice-ref="txAdvice"pointcut-ref="targetMethod"/>
config>
-->
6.struts.xml,你懂的。
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFo