lombok

lombok 是一个工具型的插件吧,感觉用不用也无所谓。如果数据模型Model属性很多,代码量还挺大的。于是可以通过他来解决

lombok 注解:
    lombok 提供的注解不多,可以参考官方视频的讲解和官方文档。
    Lombok 注解在线帮助文档:http://projectlombok.org/features/index.
    下面介绍几个我常用的 lombok 注解:
        
@Data   :注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
        
@Setter:注解在属性上;为属性提供 setting 方法
        
@Getter:注解在属性上;为属性提供 getting 方法
        
@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
        
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
        
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法

1、例子: 

package com;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j;

/** 
 * @author  作者 PZhang  E-mail:pzhang@rxhui.com 
 * @date 创建时间:2017-2-15 上午10:01:39 
 * @version 1.0 
 * @parameter   
 * @return  
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Log4j
public class Student {
	private Integer id;
	private String name ;
	 public static void main(String[] args) {
		Student student = new Student(1,"zhang");
		Student student2 = new Student();
		student2.setId(1);
		student2.setName("zhang");
		boolean bool = student.equals(student2);
		System.out.println(bool);
		System.out.println(student);
		System.out.println(student2);
		log.info("xxxxxxxxxx");
	}
	
}


 

 

输出:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/%e6%9d%82%e7%89%a9/%e5%b7%a5%e4%bd%9c%e6%96%87%e6%a1%a3/%e4%bc%9a%e5%91%98%e5%88%b6%e6%8e%a5%e5%8f%a3%e6%95%b0%e6%8d%ae/lib/slf4j-jdk14-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/Program%20Files/Maven/repository/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]
true
Student(id=1, name=zhang)
Student(id=1, name=zhang)
2017-2-22 12:47:03 com.Student main
信息: xxxxxxxxxx

  

 

如果使用呢还需要jar包的引入,以及对编译工具的引入
    <dependency>
	    <groupId>org.projectlombok</groupId>
	    <artifactId>lombok</artifactId>
	    <version>1.16.12</version>
	    <scope>provided</scope>
    </dependency>		    

2、eclipse引入

 使用 lombok 是需要安装的,如果不安装,IDE 则无法解析 lombok 注解。先在官网下载最新版本的 JAR 包,现在是 0.11.2 版本,我用的是 0.11.0
    第一次使用的时候我下载的是最新版本的,也就是我现在用的 0.11.0,到现在已经更新了两个版本,更新的好快啊 ... ...

1. 双击下载下来的 JAR 包安装 lombok
    我选择这种方式安装的时候提示没有发现任何 IDE,所以我没安装成功,我是手动安装的。如果你想以这种方式安装,请参考官网的视频。

2.eclipse / myeclipse 手动安装 lombok
    1. 将 lombok.jar 复制到 myeclipse.ini / eclipse.ini 所在的文件夹目录下
    2. 打开 eclipse.ini / myeclipse.ini,在最后面插入以下两行并保存:
        -Xbootclasspath/a:lombok.jar
        -javaagent:lombok.jar
    3.重启 eclipse / myeclipse

3、idea的引入

 

IDEA下使用时,可以通过插件的形式安装,插件下载地址:https://github.com/mplushnikov/lombok-intellij-plugin/releases 

然后

Plugins -> Install plugin from disk... 选择下载的zip包安装,重启idea即可。

另外,还有一个关键设置:

为了让设置生效,建议再重启一次idea,然后就可以开心的编码了,可以ide里可以直接看到生成的方法:(下图中打红圈的都是自动生成的)

  

 

 还可以

直接plugin超市安装

 

 

 各种注解的详细用法,请参考:https://projectlombok.org/features/index.html

 对于log的支持

@CommonsLog  
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);  
@Log  
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());  
@Log4j  
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);  
@Log4j2  
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);  
@Slf4j  
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);  
@XSlf4j  
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class); 

  日志是个好东西,大家可以看看https://my.oschina.net/pingpangkuangmo/blog/410224

 

 

 

 

 

 

 

posted @ 2017-02-22 13:01  我的天啊~  阅读(632)  评论(0编辑  收藏  举报