Java Remove repeated try, catch, finally boilerplate from DAO

前端 未结 4 1027
心在旅途
心在旅途 2021-01-06 08:06

I have a DAO class with many methods that have a lot of repeated code along the lines of: -

public void method1(...) {
  Connection conn = null;
  try {
             


        
4条回答
  •  天命终不由人
    2021-01-06 08:41

    I would use AOP for a commong logging pattern here. For example:-

      
        
                
                
                    
                  
        
    

    And the ExceptionLogger class could be something like below:-

    public class ExceptionLogger {
        private static Logger logger = Logger.getLogger(ExceptionLogger.class);
        public void logIt(JoinPoint jp, Exception e) {
            StringBuilder msg = new StringBuilder();
            msg.append("");
            logger.error(msg.toString());
        }
    }
    

提交回复
热议问题