首页 > 其他 > 详细

21_AOP_Advice增强2(异常、引介)

时间:2017-01-20 12:24:43      阅读:287      评论:0      收藏:0      [点我收藏+]

【异常抛出增强】

异常抛出异常最适合的应用场景:事务管理。

当参与事务的某个Dao发生异常时,事务管理器就必须回滚事务。

 

【异常抛出增强 例子】

技术分享

【操作数据库的Dao类:PersonDao.java】

package com.Higgin.part2;

import java.sql.SQLException;
/**
 * 模拟操作数据库并发生异常
 */
public class PersonDao {
    //查询所有Person
    public void getAllPerson(){
        throw new RuntimeException("运行时异常...");
    }
    
    //删除所有Person
    public void deleteAllPerson() throws Exception{
        throw new SQLException("删除数据异常...");
    }
}

 

【抛出异常增强(事务管理器):TransactionManager.java】

package com.Higgin.part2;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

/**
 * 抛出异常增强:事务管理器
 * 实现的接口:ThrowsAdvice   
 * ThrowsAdvice异常抛出接口没有定义任何方法,是一个标识接口,在运行期间Spring使用反射机制自行判断
 */
public class TransactionManager implements ThrowsAdvice{

    /**
     * 我们必须使用 void afterThrowing(...)方法
     * 方法名:必须为afterThrowing
     * 方法入参:前三个入参Method method,Object[] args,Object target可选(要么三个都提供,要么都不提供),
     *               最后一个入参是Throwable或其子类(这里用了子类Exception)
     *  合法的例子:afterThrowing(SQLException ex)
     *            afterThrowing(RuntimeException ex)
     *            afterThrowing(Method method,Object[] args,Object target,RuntimeException ex)
     */
    public void afterThrowing(Method method,Object[] args,Object target,Exception ex){
        System.out.println("------抛出异常增强------");
        System.out.println("method Name=="+method.getName());
        System.out.println("获取抛出异常的信息:"+ex.getMessage());
        System.out.println("成功滚回事务");
    }
}

【Spring配置的文件:part2.xml】

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">
    
    <!-- 要增强的目标对象 -->
    <bean id="target" class="com.Higgin.part2.PersonDao"/>

    <!-- 抛出异常的增强  -->
    <bean id="transactionManager" class="com.Higgin.part2.TransactionManager"/>
    
    <!-- Spring代理工厂的成员变量配置(注意这里没有p:proxyInterfaces接口属性配置)-->
    <bean id="personDao" class="org.springframework.aop.framework.ProxyFactoryBean"
        p:interceptorNames="transactionManager"
        p:target-ref="target"
        p:proxyTargetClass="true"
    />
    
</beans>

【测试类:TestTransactionManager.java】

package com.Higgin.part2.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.Higgin.part2.PersonDao;

public class TestTransactionManager {
    public static void main(String[] args) throws Exception {
        ApplicationContext context=new ClassPathXmlApplicationContext("part2.xml");
        PersonDao personDao=(PersonDao) context.getBean("personDao");
        
        //分别执行下面两条
        personDao.deleteAllPerson();
        personDao.getAllPerson();
    }
}

【运行personDao.deleteAllPerson() 结果】

技术分享

 

【运行personDao.getAllPerson() 结果】

技术分享

 【分析】

关于标识接口(比如ThrowsAdvice抛出异常增强接口)

  标识接口是没有任何方法和属性的接口。标识接口不对实现者有任何语义上的要求,仅仅表明它的实现类是属于一个特定的类型。JAVA使用标识接口来标识某一类对象,第一,通过标识接口标识同一类型的类,这些类本身可能并没有具有相同的方法。第二,通过标识接口是程序或JVM采取一些特殊处理,如java.io.Serilizable,告诉JVM对象可以被序列化。

21_AOP_Advice增强2(异常、引介)

原文:http://www.cnblogs.com/HigginCui/p/6322283.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!