Lombok

Lombok中关于@Data的使用 坑

随声附和 提交于 2019-11-27 00:17:46
当你在使用 Lombok 的 @Data 注解时,其实会有一些坑需要关注,今天就让我们来见识一下。 Lombok 先来简单介绍一下 Lombok ,其官方介绍如下: Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code. 大致意思是 Lombok 通过增加一些"处理程序",可以让 Java 代码变得简洁、快速。 Lombok 提供了一系列的注解帮助我们简化代码,比如: 注解名称功能 @Setter 自动添加类中所有属性相关的 set 方法 @Getter 自动添加类中所有属性相关的 get 方法 @Builder 使得该类可以通过 builder (建造者模式)构建对象 @RequiredArgsConstructor 生成一个该类的构造方法,禁止无参构造 @ToString 重写该类的 toString() 方法 @EqualsAndHashCode 重写该类的 equals() 和 hashCode() 方法 @Data 等价于上面的 @Setter 、 @Getter 、 @RequiredArgsConstructor 、

带你搭一个SpringBoot+SpringData JPA的环境

痞子三分冷 提交于 2019-11-27 00:14:14
前言 只有光头才能变强。 文本已收录至我的GitHub仓库,欢迎Star: https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Data JPA了解多少,如果你已经学过Spring和Hibernate的话,那么SpringBoot和SpringData JPA可以分分钟上手的。SpringBoot和SpringData JPA的好处我就不说了,当时我学习的时候也 粗略 做过笔记,有兴趣的同学可以去看看 SpringBoot就是这么简单 SpringData JPA就是这么简单 其实我在学完SpringBoot和SpringData JPA了之后,写过一个小Demo,但一直没发出来而已(懒)。 而 最近要写毕业设计的一个管理模块(CRUD),并且我又没写过相关SpringBoot和SpringData JPA的 搭建 教程,所以就诞生了这篇文章了。 一、从零搭建环境 本次我使用的是 IDEA 编辑器来搭建SpringBoot和Spring Data JPA环境 首先,我们在IDEA新建项目的时候,选择Spring Initializr,然后next就行了。 然后填写一些项目的资料(其实这些资料也无关紧要,自己看着填就好了),随后点击next 随后在勾选的时候,我就 随手 勾选了个LomBok(其他的没勾选

How does lombok work?

喜夏-厌秋 提交于 2019-11-26 21:19:15
I met lombok today. I'm very anxious to know how it works. A Java Geek Article gives some clues but it's not perfectly clear to me: Java 6 removes apt and make javac able to manage annotations, streamlining the process to obtain a simpler single step computing. This is the path taken by Lombok. Maybe with Java 6 the compile process will be: javac -> apt -> lombok apt process -> read class files and add set/get methods using ASM ? Could you show me more details about the mechanism? Lombok does indeed code against internal API, as Sean Patrick Floyd said. However, as lombok is ONLY involved in

SpringBoot:如何优雅地处理全局异常?

ε祈祈猫儿з 提交于 2019-11-26 20:13:18
之前用springboot的时候,只知道捕获异常使用try{}catch,一个接口一个try{}catch,这也是大多数开发人员异常处理的常用方式,虽然屡试不爽,但会造成一个问题,就是一个Controller下面,满屏幕的try{}catch,看着一点都不优雅,一点都不符合小明的气质,憋了这么久,小明今天终于决定对所有异常实施统一处理的方案。 开发准备 JDK8、正常的springboot项目 代码编写 通用异常处理 其实Spring系列的项目全局异常处理方式早已存在,只不过我们一直忙于搬砖,很少停下脚步去审视这个日夜与我们相伴的朋友。为了贴合主题,本次主要针对SpringBoot全局异常处理进行举例说明。 SpringBoot中有一个 @ControllerAdvice 的注解,使用该注解即表示开启全局异常捕获,接下来我们只需在自定义的方法上使用 @ExceptionHandler 注解,并定义捕获异常的类型,对这种类型的异常进行统一的处理。 举个例子: 假如我们需要针对NullException(空指针异常,是Java程序员最痛恨的异常,没有之一)进行全局处理(如下所示)。 @RestControllerAdvice public class GlobalExceptionHandler { /** * 处理空指针的异常 * @param req * @param e *

Specifying order of annotation processors

霸气de小男生 提交于 2019-11-26 20:09:32
问题 I'm trying to run Dagger 2 as well as Lombok on my Java project. Lombok has to run first, of course, but whether it actually does seems to be up to chance. At first I suspected I could specify the order by the respective position of the library jars in the class path, but that order evidently gets ignored. Is there a way to specify the order for them to run somehow, or do I simply have to live with not being able to combine two APs ? I have produced an SSCCE test case. A simple git clone &

Lombok is not generating getter and setter

折月煮酒 提交于 2019-11-26 19:05:05
问题 I just tried to send a Maven-based project to another computer and HORROR, red markers everywhere!! However, mvn clean install is building just fine. Quickly, I noticed that Lombok is not generating getters and setters for my classes, although the @Getter and @Setter are being correctly recognised by Eclipse. Both computers use the same Maven version (3.0.4) but different JDKs (1.6_23 and 1.6_33). They both use Eclipse Indigo 32 bit. Do you have an idea about how to solve the problem? 回答1:

sonarqube + lombok = false positives

我们两清 提交于 2019-11-26 18:25:32
问题 import lombok.Data; @Data public class Filter { private Operator operator; private Object value; private String property; private PropertyType propertyType; } For code above there are 4 squid:S1068 reports about unused private fields. (even they are used by lombok generated getters). I've seen that some fixes related to support of "lombok.Data" annotation have been pushed, but still having these annoying false positives. Versions: SonarQube 6.4.0.25310 SonarJava 4.13.0.11627 SonarQube scanner

Kotlin doesn't see Java Lombok accessors?

回眸只為那壹抹淺笑 提交于 2019-11-26 18:13:48
问题 Using Kotlin 1.0.0 release (compiling in IntelliJ 15). println(myPojoInstance.foo) When it tries to compile code (in IntelliJ or Gradle) that references Lombok based POJOs it gives the error "Cannot access 'foo': it is 'private' in "MyPojo". Which is true, they're all private and my object has @Value @Builder for lombok annotations. I've tried specifically calling getFoo(), but it says "unresolved reference for getFoo". There's perhaps some trick to make Kotlin aware of how to handle the

Cannot make Project Lombok work on Eclipse (Helios)

為{幸葍}努か 提交于 2019-11-26 15:11:36
I have followed the tutorial here http://projectlombok.org/ but after adding import and @Data nothing happens. Does it work on eclipse helios ? VonC Did you add -vmargs ... -javaagent:lombok.jar -Xbootclasspath/a:lombok.jar to your eclipse.ini ? Because if you have (and if you have added the lombok.jar to the libraries used by your project), it works just fine with Eclipse Helios: Ian Tegebo mentions in the comments that: a simple "restart" was not sufficient to pick up the changed vmargs: I needed to explicitly exit and then start again . chrisjleu mentions in the comments : If you happen to

lombok介绍、使用方法和总结

僤鯓⒐⒋嵵緔 提交于 2019-11-26 14:46:59
lombok是一个可以帮助我们简化java代码编写的工具类,尤其是简化javabean的编写,即通过采用注解的方式,消除代码中的构造方法,getter/setter等代码,使我们写的类更加简洁,当然,这带来的副作用就是不易阅读…不过,还是能看得懂吧,废话不多说,先看一下lombok支持的一些常见的注解。 @NonNull @Cleanup @Getter/@Setter @ToString @EqualsAndHashCode @NoArgsConstructor/@RequiredArgsConstructor /@AllArgsConstructor @Data @Value @SneakyThrows @Synchronized @Log @NonNull 这个注解可以用在成员方法或者构造方法的参数前面,会自动产生一个关于此参数的非空检查,如果参数为空,则抛出一个空指针异常,举个例子来看看: //成员方法参数加上@NonNull注解 public String getName(@NonNull Person p){ return p.getName(); } 实际效果相当于: public String getName(@NonNull Person p){ if(p==null){ throw new NullPointerException("person"); }