1. struts 2 与 spring2
struts2 本身就采用了类似于spring的IOC容器机制,可以利用struts2官方提供的插件struts2-spring-plugin-2.0.11.1.jar,直接与spring进行整合,配置文件中只需要设置
struts.objectFactory=spring
这样,就将
struts2
的对象管理交给了
spring2
的
IOC
容器。
在
struts.xml
中配置的
action
<
package
name
=
"maintaince"
extends
=
"struts-default"
>
<
action
name
=
"serverInfoList"
class
=
"serverInfoService"
method
=
"getAllServersInfo"
>
<
result
name
=
"list"
>
/jsp/server_info/server_info_list.jsp
</
result
>
</
action
>
在
spring
的配置文件中配置的
bean
<
bean
id
=
"serverInfoService"
class
=
"com.wod.service.impl.ServerInfoServiceImpl"
>
<
property
name
=
"serverInfoDao"
ref
=
"serverInfoDao"
/>
<
property
name
=
"sib"
ref
=
"serverInfoBean"
/>
</
bean
>
可以看出,struts2可以直接只用在spring2中配置的bean,引用过来作为action。
这样struts2就可以跑在spring2里面了.
另外,在web.xml中还有这些内容:
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
classpath:properties/work-assistant*.xml
</
param-value
>
</
context-param
>
加载
spring
的配置文件
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
设置
spring
的
context listener
<
filter
>
<
filter-name
>
struts2
</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
struts2
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
设置
struts2
的
dispatcher
。
2. hibernate3 与 spring2
spring 与 hibernate结合的时候,配置文件修改比较多。首先是hibernate的自身的配置被集成到了spring的配置文件中了.
1. 配置datasource:
<
bean
id
=
"dataSource"
class
=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName
=
"${hibernate.connection.driver_class}"
p:url
=
"${hibernate.connection.url}"
p:username
=
"${hibernate.connection.username}"
p:password
=
"${hibernate.connection.password}"
/>
2.配置sessionFactory
<
bean
id
=
"sessionFactory"
class
=
"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
scope
=
"prototype"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
<
property
name
=
"annotatedClasses"
>
<!-- or use <property name="annotatedPackages"> -->
<
list
>
<
value
>
com.wod.bean.Application
</
value
>
</
list
>
</
property
>
<
property
name
=
"hibernateProperties"
>
<
props
>
<
prop
key
=
"hibernate.hbm2ddl.auto"
>
create
</
prop
>
<
prop
key
=
"hibernate.dialect"
>
${hibernate.dialect}
</
prop
>
<
prop
key
=
"hibernate.show_sql"
>
${hibernate.show_sql}
</
prop
>
<
prop
key
=
"hibernate.generate_statistics"
>
${hibernate.generate_statistics}
</
prop
>
</
props
>
</
property
>
<
property
name
=
"eventListeners"
>
<
map
>
<
entry
key
=
"merge"
>
<
bean
class
=
"org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"
/>
</
entry
>
</
map
>
</
property
>
</
bean
>
这两个
bean
是
spring
结合
hibernate
的最主要的两个
bean.
当这两个
bean
设置好了之后,就可以直接使用
spring
提供的
”HibernateDaoSupport”
,直接使用封装好的
hibernate
特性,非常方便
.
<
bean
id
=
"serverInfoDao"
class
=
"com.wod.db.hibernate.ServerInfoDAO"
>
<
property
name
=
"sessionFactory"
ref
=
"sessionFactory"
/>
</
bean
>
初始化一个
DAO.
public
List<ServerInfoBean> getAllServersInfo() {
List<ServerInfoBean> find = getHibernateTemplate().loadAll(ServerInfoBean.class);
return find;
}
直接调用
getHibernateTemplate()
访问数据库。
3
.
Spring
事务的设置
1
.设置
transactionManager
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref
=
"sessionFactory"
/>
2.
设置
advice
<
tx:advice
id
=
"txAdvice"
transaction-manager
=
"transactionManager"
>
<
tx:attributes
>
<
tx:method
name
=
"find*"
read-only
=
"true"
/>
<
tx:method
name
=
"get*"
read-only
=
"true"
/>
<
tx:method
name
=
"list*"
read-only
=
"true"
/>
<
tx:method
name
=
"*"
rollback-for
=
"Exception"
/>
</
tx:attributes
>
</
tx:advice
>
3
.接下来设置
AOP
<
aop:config
>
<
aop:pointcut
id
=
"businessService"
expression
=
"execution(* com.hisoft.db.hibernate.impl.*.*(..))"
/>
<
aop:advisor
advice-ref
=
"txAdvice"
pointcut-ref
=
"businessService"
/>
<
aop:aspect
id
=
"businessAspect"
ref
=
"AOPTest"
>
<
aop:before
pointcut-ref
=
"businessService"
method
=
"before"
/>
<
aop:after-returning
pointcut-ref
=
"businessService"
method
=
"after"
/>
</
aop:aspect
>
</
aop:config
>
这个的意思是说,当执行到
com.hisoft.db.hibernate.impl
这个包下面的任何类的任何方法,而且不管参数是什么,也就是说这个包下面的所有方法调用了,都要接受前面的
transactionManager
的管理。
4
.
AOP
设置
<
bean
id
=
"AOPTest"
class
=
"com.wod.common.LogHandler.AOPTest"
/>
<
aop:config
>
<
aop:pointcut
id
=
"businessService"
expression
=
"execution(* com.hisoft.db.hibernate.impl.*.*(..))"
/>
<
aop:advisor
advice-ref
=
"txAdvice"
pointcut-ref
=
"businessService"
/>
<
aop:aspect
id
=
"businessAspect"
ref
=
"AOPTest"
>
<
aop:before
pointcut-ref
=
"businessService"
method
=
"before"
/>
<
aop:after-returning
pointcut-ref
=
"businessService"
method
=
"after"
/>
</
aop:aspect
>
</
aop:config
>
定义一个切面
,
叫做
businessAspect
,引用的是我前面定义的一个叫做
AOPTest
的类
,
然后下面的两句话:
<aop:before pointcut-ref="businessService" method="before"/>
<
aop:after-returning
pointcut-ref
=
"businessService"
method
=
"after"
/>
aop:before
指的是
在调用目标方法之前要干点事情,
pointcut-ref="businessService"
就是目标的方法,在调用匹配这个
pointcut
的方法之前,会调用
method
中定义的那个方法。
基本上就是这样了
~~~