• Exception AOP 异常处理


    场景: 常见异常处理, 避免try {}catch(){} 直接处理,  使代码更加清晰

    使用: 

    1.依赖:

           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>

    2. 避免直接切方法--> 这使用一个exceptionHandlerUtils, 然后切片切utils

    exceptionHandlerUtils:

    @Slf4j
    @Component
    public class ExceptionHandlingUtils {
       
       
        public void throwDAOException(Exception e){
            e.printStackTrace();
            log.error("Failed to operate booking into Mysql, caused by: {}",e.getMessage(),e);
            throw new DAOException(e.getMessage(),e);
        }
        
        public void printEmailException(Exception e,String subject, String to) {
            e.printStackTrace();
            log.error("***EmailException,Failed to send email {} to {}***, caused by: {}",subject,to,e.getMessage());
        }
        
        public void throwJSONConvertException(Exception e) {
            e.printStackTrace();
            log.error("Exception encountered at read json tree, caused by: {}",e.getMessage(),e);
            throw new JSONConvertException(e.getMessage(),e);
        }
    }

    3. AOP 切片:

    ExceptionHandlingAspect
    @Aspect
    @Configuration
    @Slf4j
    public class ExceptionHandlingAspect {
    
        @Value("${mail.common.from}")
        private String from;
        @Value("${mail.common.to}")
        private String to;
        @Autowired
        private MesgHelper mesgHelper;
        
    /**
     * @param joinPoint
     * @param ex  ,获取的异常
     */
        @AfterThrowing(pointcut = "execution(* com.icil.elsa.auo.common.util..*(..))", throwing = "ex")
        public void afterThrowingCommon(JoinPoint joinPoint, Throwable ex){
            log.error("Exception was detected, caused by {}",ex.getMessage());
            if(ex instanceof DAOException){
                commonProcess(ex);
            }
            else if(ex instanceof RestClientException){
                commonProcess(ex);
            }
            else if(ex instanceof ConnectionException){
                commonProcess(ex);
            }
         
        }
       
        
        private void commonProcess(Throwable ex) {
            log.error("Processing exception with name {}",ex.getClass().getSimpleName());
            Object[] paras = {DateUtils.getBeiJingDateLoc()};
            String subject = mesgHelper.getMessage("exception.handling.common.subject", paras, null);
            String exSimpleName = ex.getClass().getSimpleName();
            String body = ex.getMessage();
            Object[] params = { exSimpleName, body};
            String content = mesgHelper.getMessage("exception.handling.common.content", params, null);
            MailUtils.sendMail(to,from,subject,content);
        }
    }

    4.使用: 

    try {
     //doing something 
    }catch(Exception e ){
    exceptionHandlingUtils.throwDAOException(e);
    }  
  • 相关阅读:
    OpenGL, Net 2005, error C2381: 'exit' : redefinition; __declspec(noreturn) differs
    24点游戏
    一个3*3的格子,给定n>=10, 将[1,n]中的数填入方格中,使得相邻方格的数的和为质数
    最大子矩阵问题 PKU 1050
    12年9月12日
    再谈MSDN Library For Visual Studio 2010
    漫谈.NET开发中的字符串编码
    《.NET 4.0网络开发入门之旅》4:与Socket的第一次“约会”
    “.NET 4.0 网络开发入门之旅系列文章”—— IP 知多少?(下)
    C#之int挑战Java之Integer
  • 原文地址:https://www.cnblogs.com/lshan/p/11271827.html
Copyright © 2020-2023  润新知