随笔-31  评论-2  文章-0  trackbacks-0

web.xml的配置

在web.xml中添加

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext-resources.xml
classpath:/applicationContext-dao.xml
classpath:/applicationContext-service.xml
classpath*:/applicationContext.xml
/WEB-INF/applicationContext*.xml
/WEB-INF/security.xml
/WEB-INF/dealer-security.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

事务的配置

<aop:config>
<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))" order="0" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>

ApplicationContext.xml的配置(spring bean 的配置)

<!--ProductManager-START-->
<bean id="productManager" class="com.eryiju.service.product.impl.ProductManagerImpl">
<constructor-arg ref="productDao" />
<property name="brandDao" ref="brandDao"></property>
</bean>

<!--ProductManager-END-->
<bean id="productDao" class="com.eryiju.dao.product.impl.ProductDaoHibernate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="brandDao" class="com.eryiju.dao.product.impl.BrandDaoHibernate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

常见问题

如何与hibernate整合

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=com.eryiju.util.cache.EhCacheProvider
hibernate.show_sql=false
hibernate.connection.release_mode=after_statement
hibernate.cglib.use_reflection_optimizer=false
hibernate.search.default.directory_provider=org.hibernate.search.store.FSDirectoryProvider
hibernate.search.default.indexBase=/opt/dev/static/index
hibernate.jdbc.batch_size=50
hibernate.jdbc.fetch_size=50
</value>
<!-- Turn batching off for better error messages under PostgreSQL -->
</property>
</bean>
h2. (1)使用spring过滤器解决中文问题

在web.xml中添加:
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

h2. (2)将applicationContext.xml分解成多个文件

applicationContext-common.xml
"dataSource"
"sessionFactory"
事务相关

applicationContext-dao.xml
UserDAO

applicationContext-biz.xml
UserManager

applicationContext-action.xml
Action

struts-config.xml中要作修改:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext-*.xml" />
</plug-in>
h2. Spring2.0中的注解实现事务管理

第一步:引入<tx:>命名空间 ,在spring的配置文件中修改, beans根元素里多了三行,如下
Xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

第二步:在spring的配置文件中修改,将所有具有@Transactional 注解的bean自动配置为声明式事务支持
Java代码

<!--JDBC事务管理器,根据你的情况使用不同的事务管理器,如果工程中有Hibernate,就用Hibernate的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- 用注解来实现事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

第三步: 在接口或类的声明处 ,写一个@Transactional. 要是只的接口上写, 接口的实现类就会继承下来.
接口的实现类的具体方法,还可以覆盖类声明处的设置.
Java代码

@Transactional
public class TestPOAOImpl extends POAOBase implements TestPOAO
{
@Transactional(isolation = Isolation.READ_COMMITTED)
public void test1()
{
String sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解赵云',30)";
execute(sql);

sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解张飞',26)";
execute(sql);

int a = 9 / 0; //异常

sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解关羽',33)";
execute(sql);
System.out.println("走完了");
}
//execute() 方法略...
}

注意的几点:

1 @Transactional 只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能.
2 默认情况下,一个有事务方法, 遇到RuntiomeException 时会回滚 . 遇到 受检查的异常 是不会回滚 的. 要想所有异常都回滚,要加上 @Transactional( rollbackFor={Exception.class,其它异常}) .
posted on 2009-07-02 09:40 xiaoxinchen 阅读(177) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: