- 详细配置请看xml版,此版本只解释注解意义
web.xml
- 不变
applicationContext.xml
- 开启注入注解
<context:annotation-config/>
- 指定注入扫描包
<context:component-scan base-package="com.dao"></context:component-scan>
<context:component-scan base-package="com.biz"></context:component-scan>
<context:component-scan base-package="com.action"></context:component-scan>
- 开启事务注解
<tx:annotation-driven transaction-manager="txManager" />
- 注意 一下内容还要写入applicationContext.xml中
- 数据库的连接
- sessionFactory的配置
- 事务的创建
struts.xml
- 默认支持注解
- 只需配置非注解配置
开始注解
dao层
- 配置dao自动装配,并指定名称(spring)
@Service("StudentDAO")
biz
- 配置biz自动装配,并指定名称(spring)
@Service("StudentBiz")
- 配置biz为事务添加点(spring)
@Transactional
- 指定biz的dao对象注入dao层对应的名称(spring)
@Resource(name="StudentDAO")
- 注意:要写到注入对象之上
action层
- 配置action层自动装配(spring)
@Controller
- 配置action层的继承的包(struts2)
@ParentPackage("struts-default")
- 指定action层所在的空间(struts2)
@Namespace("/")
- 指定action的biz对象注入action层对应的名称(spring)
@Resource(name="StudentBiz")
- 指定提交的方法–以及返回得结果的去向(struts2)
@Action(value="save_Student",results={@Result(name="ok",location="${path}",type="redirect")
})
- 注意:要写到注入对象之上
大功告成,非常简单