Lombok

从今天起让我们忘记Java中的get/set方法吧!

旧巷老猫 提交于 2019-11-26 14:46:46
曾几何时,我们写代码的时候,每次写Bean的时候都会使用快捷键生成get/set方法,有时候我经常会想,既然每一个Bean我们都会给其提供get/set方法,那么为什么还有会这个东西哪?(当然这只是一个问题,毕竟还是很有用的,这一点就是Java语言中的private、public等作用域) 今天说的当然不是这个,我们讨论的是希望有一种优雅的方式编写我们的get/set方法或者说是一种优雅的简化。那我们就说一下今天的主角吧! Lombok 如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。 Java代码简化神器!就是如此美妙! Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。 https://projectlombok.org/ https://github.com/rzwitserloot/lombok 如何使用 1、安装IDEA插件Lombok Plugin 2、引入pom,版本为1.16.18 3、简单使用,设置get/set方法 4、稍微复杂一点的使用 其他使用

SpringBoot | 第二章:lombok介绍及简单使用

巧了我就是萌 提交于 2019-11-26 14:46:25
在去北京培训的时候,讲师说到了 lombok 这个第三方插件包,使用了之后发现,确实是个神奇,避免了编写很多臃肿的且定式的代码,虽然现代的 IDE 都能通过快捷键或者右键的方式,使用 Generate Getters and Setters 快速生成setters/getters,但当某一个字段修改或者添加字段时,又需要重复的操作一遍,但使用了 lombok 之后。一切都是自动的,除了最常用的生成 setters/getters ,还有诸如:自动生成 toString 方法、 equals 、·haashcode·等,还能快速生成 Builder模式 的javabean类,实在是方便。程序猿是很懒的,一切重复的工作都想通过脚本或者自动化工具来完成,所以,使用 lombok 吧。 为何要使用Lombok 我们在开发过程中,通常都会定义大量的JavaBean,然后通过IDE去生成其属性的构造器、getter、setter、equals、hashcode、toString方法,当要增加属性或者对某个属性进行改变时,比如命名、类型等,都需要重新去生成上面提到的这些方法。这样重复的劳动没有任何意义,Lombok里面的注解可以轻松解决这些问题。 简化冗余的JavaBean代码,使得实体文件很简洁。 大大提高JavaBean中方法的执行效率,省去重复的步骤 Lombok简介

SpringBoot学习之路:13.SpringBoot使用Lombok简化JavaBean代码

混江龙づ霸主 提交于 2019-11-26 14:46:02
SpringBoot采用约定大于配置的方式,极大的减少了配置文件的使用,简化了开发过程中的配置难度,使得项目开发更加高效。当然初次之外我们还可以采用其他手段来简化代码,使得代码没那么臃肿。 开发JavaWeb的同学都知道,一个标准的JavaBean组件由属性、方法、构造函数三部分组成,其中JavaBean需要提供可读写的属性,也就是属性的setter和getter方法,而这些使得代码看起来很臃肿,但却是必不可少的。 同样在Springboot项目中JavaBean组件也是无处不在,我们今天主要说说SpringBoot项目中使用Lombok插件简化JavaBean的代码,以及开发ide安装Lombok插件等 1.项目中引入Lombok开发包 我们使用maven构建的springboot项目,引入Lombok只需要在pom文件中加入以下依赖: <!--lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> 此时我们编写一个javabean加上lombok的注解看看: 虽然引入了lombok开发包,但是idea开发工具依然不认识(Private field 'xxx' is

how to configure lombok in eclipse luna

我怕爱的太早我们不能终老 提交于 2019-11-26 14:19:23
问题 I configure lombok in eclipse Luna with Maven. Annotation is added properly, but no getter and setter are generated. eclipse.ini `-vm E:\Program Files\Java\jdk1.7.0_60\bin` `-vmargs` `-Dosgi.requiredJavaVersion=1.7` `-javaagent:F:\Tools\Java Lib\Lombok\lombok.jar` `-Xbootclasspath/a:F:\Tools\Java Lib\Lombok\lombok.jar` `-Xms40m` `-Xmx512m` 回答1: Disclosure: I am one of the lombok developers. I might be biased :-) I strongly suggest installing Lombok via executing the lombok jar: java -jar

Lombok annotations do not compile under Intellij idea

我的梦境 提交于 2019-11-26 12:49:15
I've installed the plugin for intellij idea(lombok-plugin-0.8.6-13). Added lombok.jar into classpath I can find getters and setters in the window of structure. And Intellij shows no error. Setting - Lombok plugin - Verified Intellij configuration for lombok, it shows that "configuration of IntelliJ seems to be ok". It seems everything is OK. But when I compile a test, errors come: can not find the methods getXXX and setXXX. I opened the .class file with IntelliJ and find out that there is no setXXX and getXXX methods. Could somebody tell me why? plugin:lombok-plugin-0.8.6-13 lombok.jar:1.14.0

Can&#39;t compile project when I&#39;m using Lombok under IntelliJ IDEA

浪尽此生 提交于 2019-11-26 12:41:51
I'm trying to use Lombok in my project that I'm developing using IntelliJ IDEA 11. I've installed 3rd-party plugin for IDEA and it seems working fine because IDEA sees all autogenerated methods/fields. So I have a class that uses Slf4j. I annotated it like this import lombok.extern.slf4j.Slf4j; @Slf4j public class TestClass { public TestClass() { log.info("Hello!"); } } But when I build my project compiler spits: cannot find symbol variable log . Could you please tell me what I'm missing here ? Update: It turned out it's RequestFactory annotation process that fails. input files: {com.zasutki

Compiling a Java Class in memory with `lombok` annotations and Java JDK 8

笑着哭i 提交于 2019-11-26 11:37:58
问题 I\'m trying to retrieve the description of a few Java Beans from an XML file. I\'d like to annotate them with @Data from project lombok to automatically include constructor, equals, hashCode, getters, setters and toString. I\'d like to compile them in memory, generate a few instances (with data from the same XML file) and add them to Drools to eventually do some reasoning on that data. Unfortunately, I cannot compile those classes and so I am asking for your help! The following code shows how

Can&#39;t make Jackson and Lombok work together

蓝咒 提交于 2019-11-26 10:39:29
问题 I am experimenting in combining Jackson and Lombok. Those are my classes: package testelombok; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; import lombok.Value; import lombok.experimental.Wither; @Value @Wither @AllArgsConstructor(onConstructor=@__(@JsonCreator)) public class TestFoo { @JsonProperty(\"xoom\") private String x; private int z; } package testelombok; import com.fasterxml.jackson

Java SneakyThrow of exceptions, type erasure

删除回忆录丶 提交于 2019-11-26 09:44:53
问题 Can someone explain this code? public class SneakyThrow { public static void sneakyThrow(Throwable ex) { SneakyThrow.<RuntimeException>sneakyThrowInner(ex); } private static <T extends Throwable> T sneakyThrowInner(Throwable ex) throws T { throw (T) ex; } public static void main(String[] args) { SneakyThrow.sneakyThrow(new Exception()); } } It may seems strange, but this doesn\'t produce a cast exception, and permits to throw a checked exception without having to declare it in the signature,

Is it safe to use Project Lombok? [closed]

ぐ巨炮叔叔 提交于 2019-11-26 09:14:31
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . In case you don\'t know Project Lombok helps with some of the annoyances of Java with stuff like generating getters and setters with annotations and even simple JavaBean like generation with @Data. It could really help me, especially in 50 different event objects where you have