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