Lombok

lombok @Accessors用法

亡梦爱人 提交于 2020-01-08 10:14:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> @Accessors 翻译是存取器。通过该注解可以控制getter和setter方法的形式。 fluent 若为true,则getter和setter方法的方法名都是属性名,且setter方法返回当前对象。 @Data @Accessors(fluent = true) class User { private Integer id; private String name; // 生成的getter和setter方法如下,方法体略 public Integer id(){} public User id(Integer id){} public String name(){} public User name(String name){} } chain 若为true,则setter方法返回当前对象 @Data @Accessors(chain = true) class User { private Integer id; private String name; // 生成的setter方法如下,方法体略 public User setId(Integer id){} public User setName(String name){} } prefix 若为true

slf4j 是怎么绑定具体的日志框架的

两盒软妹~` 提交于 2020-01-07 18:57:49
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> SLF4J 英文全称是 Simple Logging Facade for Java, 是一个门面(外观)接口或者说各种日志框架的抽象,比如 java.util.logging, logback, log4j 等;使用这货,我们可以不用关心具体的实现,也就是说可以随时切换日志框架。 这边使用的是目前最新版本的 slf4j 1.8.0-beta2 简单使用下试试 示例代码 https://github.com/minorpoet/logging-slf4j 添加依赖 dependencies { compile group: 'org.slf4j', name: 'slf4j-api', version: '1.8.0-beta2' } 使用 package pri.holysu.logging.sl4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloWorld { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(HelloWorld.class); logger.info("hello

相同类中方法间调用时日志Aop失效处理

女生的网名这么多〃 提交于 2020-01-07 06:46:38
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 本篇分享的内容是在相同类中方法间调用时Aop失效处理方案,该问题我看有很多文章描述了,不过大多是从事务角度分享的,本篇打算从日志aop方面分享(当然都是aop,失效和处理方案都是一样),以下都是基于springboot演示; 快速定义个日志Appender 快速定义个拦截器和日志注解(aop) 模拟相同类中方法间调用时aop失效 Aop失效处理方案(就两种足够了) 快速定义个日志Appender 日志我还是喜欢log4j,大部分朋友也同样吧,这里lombok与log4j结合来完成我们的日志,如下maven包(最新mvn还是建议去官网找): <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.0-alpha0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12<

恕我直言,在座的各位根本写不好Java!

▼魔方 西西 提交于 2020-01-07 06:29:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 其实,本不想把标题写的那么恐怖,只是发现很多人干了几年 Java 以后,都自认为是一个不错的 Java 程序员了,可以拿着上万的工资都处宣扬自己了,写这篇文章的目的并不是嘲讽和我一样做 Java 的同行们,只是希望读者看到此篇文章后,可以和我一样,心平气和的争取做一个优秀的程序员。 文章核心 讲述方向 由于一直从事移动互联网相关工作,Java 开发中经常和移动端打交道或者做一些后端的工作,所以本篇文章更可能涉及和移动端的交互或者与后端的交互方式,笔者希望以自身的一些学习经验或者开发经验,可以带动认真阅读本篇文章的读者们,让大家对 Java 有一个更好的态度去学习它,它不只是一个赚钱的工具而已。 笔者身边有很多与笔者年龄相仿或年龄更大的朋友或同事,经常有人问我:“你现在还在学习吗?我觉得没什么好学的,这些东西都差不多”,我总是回答只要有时间,我就要看一会书,这个时候,大家都会露出一副不屑的眼神或笑容。 其实,非常能理解身边朋友或同事的看法,以目前状态来讲,大多都是工作至少 5 年的程序员了,对于公司大大小小的业务需要,以目前的知识储备来讲,都可以轻松应对,“没有什么好学的”其实这句话没有多大的问题,但是,如果你对编程还有一点点兴趣,只是不知道如何努力或改进,希望本篇文章可以帮到你。 技术点 本文不是一个吹嘘的文章

建造者模式(Builder)

让人想犯罪 __ 提交于 2020-01-07 01:45:40
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 模仿lombok,照猫画虎,实现自己的建造者模式 使用静态内部类实现 package com.depen.builder; import javafx.util.Builder; /** * @Description:模仿lombok建造这模式 * @Author :YanDepeng * @Date :Created in 2020/1/6 20:20 * @Version : */ public class Man { private int age; private String name; private String sex; public static Man_Builder builder() { return new Man_Builder(); } private Man(int age, String name, String sex) { this.age = age; this.name = name; this.sex = sex; } @Override public String toString() { return "Man{" + "age=" + age + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; }

IDE does not show getters and setters generated by Lombok for a Jackson annotated class

こ雲淡風輕ζ 提交于 2020-01-06 08:01:12
问题 I use Intellij Idea 2019.1.2 community edition for my Java projects. I have a Jackson 2.9.6 annotated POJO which uses Lombok 1.18.0 to generate the getters and setters for the pojo. I have some "client" code to deserialize a sample Json text to the Pojo class. The deserialization works fine, without any compilation issues and the class file for the pojo actually has all the getters and setters. But, the IDE does not show any getters and setters for the pojo in the "client" code. Invalidating

IDE does not show getters and setters generated by Lombok for a Jackson annotated class

点点圈 提交于 2020-01-06 08:01:07
问题 I use Intellij Idea 2019.1.2 community edition for my Java projects. I have a Jackson 2.9.6 annotated POJO which uses Lombok 1.18.0 to generate the getters and setters for the pojo. I have some "client" code to deserialize a sample Json text to the Pojo class. The deserialization works fine, without any compilation issues and the class file for the pojo actually has all the getters and setters. But, the IDE does not show any getters and setters for the pojo in the "client" code. Invalidating

Validate fields only if other field is false

血红的双手。 提交于 2020-01-06 06:56:05
问题 Suppose I have an @ConfigurationProperties class which needs to validate a set of fields based on the value of another field. For example, SdkProperties has an enabled field. Only when enabled is true should the other fields be validated. SdkProperties @Configuration @ConfigurationProperties(...) @Data @Validated public class SdkProperties { private boolean enabled; @NotEmpty private String apiKey // ... etc. } The @NotEmpty annotation should only be validated if enabled is true . What's the

static import not working in lombok builder in intelliJ

与世无争的帅哥 提交于 2020-01-03 07:38:11
问题 I am using Lombok in IntelliJ. Though everything else is working fine when I try to do the static import of the Lombok builder, IntelliJ build project doesn't find the builder class. If you don't use static import, it works fine. 回答1: This is a known bug, and not something that's easy to fix. Static imports are resolved before the annotation processors are run. This is a problem in javac, not lombok. Disclosure: I am a lombok developer. 回答2: I ran into this error, after I added the lombok

Getting around Json jackson and lombok constructor requirements

流过昼夜 提交于 2020-01-03 00:56:39
问题 Using json to save and load data requires a constructor for json to load the object, and I'm having trouble getting lombok annotations to work with this. What should I do? This is what my class looked like before and after attempting to use an annotation to construct my item: @Data public class Item { //before private int id; private int amount; public Item(@JsonProperty("id") int id, @JsonProperty("amount") int amount) { this.id = id; this.amount = amount; } } @Data @AllArgsConstructor