Spring(四)基于注解配置IOC容器&基于注解实现声明式事务

一、使用注解实现IOC配置

1.定义Bean——@Component

package com.wzj.dao.impl;
import org.springframework.stereotype.Component;

@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	@Override
	public List<User> selectAllUsers() {
		//...	
	}

}
@Component("userDao")的作用=<bean id="userDao" class="UserDaoImpl"></bean>
另外spring还提供了3个特殊的注解,推荐使用特定的注解:
(1)@Repository:用户标注Dao类
(2)@Service:用于标注业务类
(3)@Controller:用于标注控制器类

2.实现自动装配——@Autowired

@Autowired可以对属性或属性的set方法进行标注,例如:
package com.wzj.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("userService")
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;


	//...
}

3.指定Bean的作用域——@Scope

package com.wzj.action;

import org.springframework.context.annotation.Scope;


@Scope("prototype")
public class UserAction extends ActionSupport{
	//...
}

4.加载使用注解定义的Bean

使用注解定义完Bean组件之后,需要在启动Spring容器时配置加载,导入context命名空间:
<?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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-4.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	">
	<!-- 加载注解标注的类 -->
	<context:component-scan base-package="com.wzj.entity"/>
</beans>

二、使用注解实现声明式事务

spring支持使用@Transactional注解来标注事务
1.仍然需要在spring配置文件中配置事务管理对象,并且添加注解配置事务的支持

<!-- 配置事务管理Bean -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 添加对注解配置的事务的支持 -->
<tx:annotation-driven transaction-manager="txManager"/>

2.在业务方法上添加@Transactional注解
package com.wzj.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class UserServiceImpl implements UserService{
	private UserDao userDao;


	/**
	 * 使用@Transactional注解标注事务
	 */
	@Transactional(readOnly=true,propagation=Propagation.REQUIRES_NEW)
	@Override
	public List<User> getAllUsers() {
		return userDao.selectAllUsers();
	}


	//...省略其他
}
默认的@Transactional设置如下:
——传播设置:PROPAGATION_REQUIRED
——隔离级别:ISOLATION_DEFAULT
——是否读写:读/写
——超时默认是依赖于事务系统的,或者事务超时没有被支持
——任何RuntimeException将触发事务回滚,但是任何checked Exception将不触发事务回滚


@Transactional注解的属性
属性类型说明
propagation枚举型:Propagation可选的传播性设置。使用举例:
@Transactional(
propagation=Propagation.REQUIRES_NEW)
isolation枚举型:Isolation可选的隔离性级别。使用举例:
@Transactional(
isolation=Isolation.READ_COMMITTED)
readOnly布尔型是否为只读型事务。使用举例:@Transactional(readOnly=true)
timeoutint型(以秒为单位)事务超时。使用举例:Transactional(timeout=10)
rollbackFor一组 Class 类的实例,必须是Throwable的子类一组异常类,遇到时 必须 回滚。使用举例:@Transactional(
rollbackFor={SQLException.class}),多个异常用逗号隔开
rollbackForClassName一组 Class 类的名字,必须是Throwable的子类一组异常类名,遇到时 必须 回滚。使用举例:@Transactional(
rollbackForClassName={
"SQLException"}),多个异常用逗号隔开
noRollbackFor一组 Class 类的实例,必须是Throwable的子类一组异常类,遇到时 必须不 回滚
noRollbackForClassName一组 Class 类的名字,必须是Throwable的子类一组异常类名,遇到时 必须不 回滚


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值