zoukankan      html  css  js  c++  java
  • lombok 使用及技巧

    @AllArgsConstructor替代@Autowired构造注入,多个bean 注入时更加清晰L
    @Slf4j
    @Configuration
    @AllArgsConstructor
    public class RouterFunctionConfiguration {
       private final HystrixFallbackHandler hystrixFallbackHandler;
       private final ImageCodeHandler imageCodeHandler;
       
    }
    
    
    @Slf4j
    @Configuration
    public class RouterFunctionConfiguration {
       @Autowired
       private  HystrixFallbackHandler hystrixFallbackHandler;
       @Autowired
       private  ImageCodeHandler imageCodeHandler;
    }
    
    @RequiredArgsConstructor相较于 @AllArgsConstructor  只会构造注入 final 注释的属性,推荐使用
    @RestController
    @RequiredArgsConstructor
    @RequestMapping("/user")
    @Api(value = "user", tags = "用户管理模块")
    public class SysUserController {
    
    	private final SysUserService userService;
    }
    
     @SneakyThrows 抛出异常
    @SneakyThrows
    private void checkCode(ServerHttpRequest request) {
       String code = request.getQueryParams().getFirst("code");
    
       if (StrUtil.isBlank(code)) {
       	throw new ValidateCodeException("验证码不能为空");
       }
    
       redisTemplate.delete(key);
    }
    
    
    // 不使用就要加这个抛出
    private void checkCode(ServerHttpRequest request) throws ValidateCodeException {
       String code = request.getQueryParams().getFirst("code");
    
       if (StrUtil.isBlank(code)) {
       	throw new ValidateCodeException("验证码不能为空");
       }
    }
    @UtilityClass工具类再也不用定义static的方法了,直接就可以Class.Method 使用 
    @UtilityClass
    public class Utility {
    
        public String getName() {
            return "name";
        }
    }
    
    public static void main(String[] args) {
        System.out.println(Utility.getName());
    }
    
    @Cleanup: 清理流对象,不用手动去关闭流,多么优雅
    @Cleanup
    OutputStream outStream = new FileOutputStream(new File("text.txt"));
    @Cleanup
    InputStream inStream = new FileInputStream(new File("text2.txt"));
    byte[] b = new byte[65536];
    while (true) {
       int r = inStream.read(b);
       if (r == -1) break;
       outStream.write(b, 0, r); 
    }
    

      

     

     

     

     
  • 相关阅读:
    安卓模拟器黑屏
    关系型数据库的1NF、2NF、3NF
    flowable数据库详解
    spring事务传播行为详解
    springboot整合activity
    各种java面试题目
    springCloud中增加gateway(超详细)
    mysql实现主从复制
    flowable整合springboot
    window安装linux系统
  • 原文地址:https://www.cnblogs.com/418836844qqcom/p/15429796.html
Copyright © 2011-2022 走看看