【spring】基于注解配置AOP

基于注解配置spring AOP

  1. 在配置文件中导入 context 的名称空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
<!-- 配置数据库操作对象 -->
<bean id="dbAssit" class="com.xxx.dbassit.DBAssit"> 
    <property name="dataSource" ref="dataSource"></property> 
    <!-- 指定 connection 和线程绑定 -->
	<property name="useCurrentConnection" value="true"></property> 
</bean>
    
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 
    <property name="jdbcUrl" value="jdbc:mysql:///spring"></property> 
    <property name="user" value="root"></property>
	<property name="password" value="1234"></property>
</bean>
</beans>
  1. 把资源使用注解配置
/**
 * 账户的业务层实现类
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
	@Autowired
	private IAccountDao accountDao;
}
/**
* 账户的持久层实现类
*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
	@Autowired
	private DBAssit dbAssit ; 
}
  1. 在配置文件中指定 spring 要扫描的包
<!-- 告知 spring,在创建容器时要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
  1. 把通知类也使用注解配置
/**
* 事务控制类
*/
@Component("txManager")
public class TransactionManager {
	//定义一个 DBAssit 
    @Autowired
	private DBAssit dbAssit ; 
}
  1. 在通知类上使用@Aspect 注解声明为切面
@Component("txManager") 
@Aspect//表明当前类是一个切面类 
public class TransactionManager {
//定义一个 DBAssit 
    @Autowired
	private DBAssit dbAssit ;
}
  1. 在增强的方法上使用注解配置通知

@Before
作用: 把当前方法看成是前置通知。
属性:

  • value:用于指定切入点表达式,还可以指定切入点表达式的引用。
//开启事务
@Before("execution(* com.xxx.service.impl.*.*(..))") 
public void beginTransaction() {
	try { 
        dbAssit.getCurrentConnection().setAutoCommit(false);
	} catch (SQLException e) { 
        e.printStackTrace();
	} 
}

@AfterReturning
作用: 把当前方法看成是后置通知。
属性:

  • value:用于指定切入点表达式,还可以指定切入点表达式的引用
/提交事务
@AfterReturning("execution(* com.xxx.service.impl.*.*(..))") 
public void commit() {
    try { 
        dbAssit.getCurrentConnection().commit();
	} catch (SQLException e) { 
        e.printStackTrace();
	}
}

@AfterThrowing
作用: 把当前方法看成是异常通知。
属性:

  • value:用于指定切入点表达式,还可以指定切入点表达式的引用
//回滚事务
@AfterThrowing("execution(* com.xxx.service.impl.*.*(..))") 
public void rollback() {
	try { 
        dbAssit.getCurrentConnection().rollback();
	} catch (SQLException e) {
		e.printStackTrace(); 
    }
}

@After
作用: 把当前方法看成是最终通知。
属性:

  • value:用于指定切入点表达式,还可以指定切入点表达式的引用
//释放资源
@After("execution(* com.xxx.service.impl.*.*(..))") 
public void release() {
	try {
		dbAssit.releaseConnection(); 
    } catch (Exception e) {
		e.printStackTrace();
	} 
}
  1. 在 spring 配置文件中开启 spring 对注解 AOP 的支持
<!-- 开启 spring 对注解 AOP 的支持 -->
<aop:aspectj-autoproxy/>

环绕通知注解配置

@Around
作用:把当前方法看成是环绕通知。
属性:

  • value:用于指定切入点表达式,还可以指定切入点表达式的引用。 /**

环绕通知


@Around("execution(* com.xxx.service.impl.*.*(..))") 
public Object transactionAround(ProceedingJoinPoint pjp) {
	//定义返回值
	Object rtValue = null; 
    try {
		//获取方法执行所需的参数
		Object[] args = pjp.getArgs(); //前置通知:开启事务 beginTransaction();
		//执行方法
		rtValue = pjp.proceed(args); 
        //后置通知:提交事务
		commit();
	}catch(Throwable e) {
		//异常通知:回滚事务 
        rollback(); 
        e.printStackTrace();
	}finally {
		//最终通知:释放资源 
        release();
	}
	return rtValue; 
}

切入点表达式注解

@Pointcut("execution(* com.xxx.service.impl.*.*(..))") 
private void pt1() {}

引用方式

@Around("pt1()")//注意括号
public Object transactionAround(ProceedingJoinPoint pjp) {
	//定义返回值
	Object rtValue = null; 
    try {
    }catch{
    }finally{
    }
    return;
}
发布了18 篇原创文章 · 获赞 0 · 访问量 271

猜你喜欢

转载自blog.csdn.net/shijyuan/article/details/104975178