`
zscomehuyue
  • 浏览: 402239 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

strust2.1 + spring3.0 + ibatis3.0 + annotation

阅读更多
strust2.1 + spring3.0 + ibatis3.0 + annotation
文章分类:Java编程
近期开发框架选择strust2.1 + spring3.0 + ibatis3.0
尽量使用annotation,少使用xml,ibatis除外

1.strust2.1
主要使用到的plugin是Convention Pluin,因为他是实现Restful和annotation的关键。
ServletActionContext静态类可以得到很多运行环境
filterServlet接收到請求,使用ActionProxy分析配置文件,进而确定这个请求执行哪些interceptors,再调用对应action完成请求,返回请求前还可能需要执行一些interceptors,所以Strust的功能都是使用interceptor方式提供的,我们要处理的只是相应的action.strust提供了默认的interceptors stack,这样每个action都不用定义这些interceptors而直接使用他们的功能,而一些其他interceptor,或自定义的 interceptor则需要在action中定义.
暂时可能用到的interceptors: file-upload,object-population,servlet-config
如果有多个interceptors 也就是interceptor stack,他们的执行顺序有时挺重要的
(1).object popublation对象自动装载params
须有modelDriven,params这两个interceptor
action implements ModelDriven
public Person getModel(){
   this.person = new Person();
   return this.person;
}

(2).servlet config
须有servletConfig interceptor,
主要是为了得到HttpServletRequest,HttpServletResponse,
action 分别 implements ServletRequestAware,ServletResponseAware

(3).file-upload
须有fileUpload interceptor
在html中
<form name="myForm" enctype="multipart/form-data">
     <input type="file" name="myDoc" value="Browse ..." />
     <input type="submit" />
</form>
在action中
public void setMyDoc(File myDoc)
public void setMyDocContentType(String contentType)
public void setMyDocFileName(String filename)
注意setMyDoc跟html中的name="myDoc"对应
上传多个"myDoc"时,就用setMyDoc(File[] myDoc),下同
Struts2的action是有状态的,必须保证其线程的安全性,
也就是一个请求new一个action,如果使用spring管理则使用@Scope("prototype")修饰action.
如果主要使用annotation配置struts的功能,action类需要用@ParentPackage("default")修饰才能用到“default"package定义的东西,
比如interceptor,global-result等.
应该也可以直接在"default"那个package上标明namespace="/"
另一个比较郁闷的地方是使用Convention后并不能include其他文件,
比如<#include "/include/meta.html">
虽然默认的根模板是/WEB-INF/content/,但是必须使用
<#include "/WEB-INF/content/include/meta.html">也就是必须指定全路径
最后的解决方法是在action类上使用@ResultPath("/WEB-INF/content/")来指定根模板目录.
convention会在没有相对应的action类定义路径时直接返回模板文件,这有时对于返回静态文件很有用,这样就不用每个請求都需要一个action类对应着.但是这时的include又必须使用全路径了.

2.spring3.0
需要额外的类库:aopalliance.jar, aspectjweaver.jar, cglib-nodep-2.1_3.jar
主要使用到事务控制,依赖注入(DI)
通过在类型字段之上定义@AutoWare,spring会自动帮我们注入类型的对象到相应变量。
在类定义上使用@Component表示这个类让spring管理,另外还有几个相似的annotation
@Service表示业务层,@Controller表示控制层(类似strust的Action),
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。

3.ibatis3.0
SqlSessionFactory
每个应用对应一个SqlSessionFactory实体对象,在整个应用过程都会存在
要构建一个SqlSessionFactory对象,可以通过SqlSessionFactoryBuilder.build(reader)构建,
reader是Reader对象,读取ibatis的配置文件如SqlMapConfig.xml

SqlSession
从SqlSessionFactory.openSession()拿到一个sqlSession,
这个sqlSession是执行SQL Mapper命令的全部,
他的应用scope应该是线程的,可以在service层控制关闭session

接着就是ibatis的配置文件,影响到ibatis的行为,定义顺序不能乱,如
导入sql mapper文件就必须放到最后,

4.关于整合strust2.1,spring3.0,ibatis3.0
strust2.1与spring3.0集成比较简单
将strust的spring插件放到lib中,设置strust常量
<constant name="struts.objectFactory" value="spring" />
strust2.1的action是无状态的,而他的interceptor才是有状态的,也就是必须线程安全的,所以Action类不需要@Scope(“prototype”),一开始我以为需要.
而Strust的核心类,interceptor,results有没有交给spring管理呢,
按道理是有的,因为strust有个专门的spring插件


ibatis3.0没有提供与spring集成的插件,
spring提供的是ibatis 2版本,
所以我自己写了一个Ibatis3SqlSessionFactoryBean,
这个bean的任务是配置ibatis的基本属性,
enviroment,transactionManager,dataSource不再配置,
使用spring提供的dataSource,事务控制留给spring去管理.
这个bean implements FactoryBean<SqlSessionFactory>, InitializingBean
在afterPropertiesSet()方法中定义怎样build这个SqlSessionFactory,
其实在build这个sqlsessionFactory的时候可以参考ibaits3本身的build实现.
问题是那个environment在ibatis的配置文件上不存在了,因为都交给spring去管理了,
所以可以自己构造environment这个对象,再set到configuration中
再return new DefaultSqlSessionFactory(configuration);
其中的关键点在于那个datasource,因为我们的事务是交给spring管理了,所以这个datasource应该包装成 dataSourceToUse = new TransactionAwareDataSourceProxy(this.dataSource);
不然的话spring不会帮我们处理事务,也就是不会commit,close之类的.
变成要调用者完成这些复杂的事.
还有个问题是从sqlSessionFactory.openSession();拿到的sqlSession,
可以将它设置成ThreadLocal,这样在一个线程中拿到的sqlSession是相同的.
//------
经测试使用ThreadLocal会发生两次请求拿到的connection相同,导致第二次抛出connection已关闭的错误
//------
还可以使用
(SqlSession)TransactionSynchronizationManager.getResource(sqlSessionFactory);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics