spring整合方案
一、数据访问
1、Spring的数据访问设计思想2、数据源配置:
方式一:Spring内置实现 DriverManagerDataSource
<bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hibdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>windows</value>
</property>
</bean>
方式二:DBCP提供的BasicDataSource
<bean id="dataSource" auth="Container" description="DB Connection"
type="javax.sql.DataSource" username="root" password="windows"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/tarena" maxActive="5" />
step2:
在context.xml中(conf\context.xml):
<ResourceLink name="jdbc/mydatasource" global="jdbc/mydatasource" type="javax.sql.DataSourcer"/>
step3:
在beans-config.xml:
<bean id="dataSource" />
</property>
</bean>
step3:配置DAO
<bean id="orderDao" />
</property>
<property name="mappingResources">
<list>
<value>lab6/Order.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
step3:配置DAO
<bean id="orderDao" />
</property>
</bean>
注意:以上配置是要求dao 继承HibernateDaoSupport
二、事务:
1、Spring事务机制
声明式事务、事务管理器
2、hibernate事务
step1: 配置数据源
step2:配置sessionfactory (同上)
step3:配置事务管理器
<bean id="myTransactionManager"
/>
</property>
</bean>
step4:创建事务服务代理
<bean id="saleService"
/>
</property>
<property name="target">
<ref bean="saleServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
注:
事务属性描述格式:
传播行为,隔离级别,只读事务(readonly),回滚规则
在默认情况下,Spring的容器对于非受查异常(服务模块中抛出的非受查异常)
,会回滚事务。对于受查异常,会提交事务。
如果即使发生了某种受查异常,也要回滚事务,可以用 “- 异常类型“来声明。
同样,对于非受查异常,如果不要求回滚事务,可以用"+异常类型"来声明
3、简化事务配置
继承、自动代理
三、Spring与struts整合:
前提:
配置ContextLoaderPlugIn插件:负责装载spring应用上下文
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
</plug-in>
方式一:通过Spring的ActionSupport类
ActionSupport类:
知道ApplicationContext的获得方式。
步骤:
1、Action直接继承ActionSupport
2、使用ApplicationContext ctx = getWebApplicationContext();取得Spring上下文
3、取得相应Bean
注意:有可能需要替换commons-attributes-compiler.jar包。
优点:
简单
缺点:
耦合高
违反IOC
无法使用多方法的Action
方式二:通过Spring的DelegatingActionProxy类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
<action path="/somepath" type="org.springframework.web.struts.DelegatingActionProxy"/>
3、在Spring配置文件中
<bean name="/somepath" />
3、在Spring配置文件中
<bean name="/somepath" class="SomeAction">
<property name="service"><ref bean=""/>
</bean>
小结:
Spring与Struts整合方式只有两种:
(1)由Spring容器来管理Action(方式二,方式三)
(2)Action处于容器之外(方式一)
注意:
中文问题:
设置过滤器,设置页面编码,数据库编码
1 楼 wazj0517 2008-04-24 好帖,学习了!! 2 楼 laowood 2008-04-26 mark
---------------
Java爱好者聚集扣扣群32351004