Java Lombok Tutorial

Java Lombok Tutorial by Online Tutorials

Lombok is Java library, which may plug into editor and supply how to avoid writing getter/setter/equal and other methods by using annotation.
Lombok has a variety of annotations that can be used inside our software that are checked during the compilation period, and an acceptable application extension will take place depending on the annotation used.

Lombok is compatible with below IDE

  • MyEclipse
  • Red Hat JBoss Developer Studio
  • JBoss Developer Studio (JBDS)
  • Spring Tools Suite (STS)

Steps to install Lombok into Intellj

  • Go to File > Settings > Plugins

Java lombok tutorials | online tutorials

  • Click On Install
  • Click on Restart

Steps to install Lombok into Eclipse

Download lombok.jar.

You can also use lombok jar from .m2 directory, once maven install lombok.jar.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    <scope>provided</scope>
</dependency>
  • Now copy lombok.jar into Eclipse folder.
  • Open command prompt from the same folder, and execute java -jar lombok.jar. It will open below wizard.

Let’s explore some Lombok Annotations.

@Val
Just specify val, rather than declaring the sort of variable like we neutralize Java as given below.
Example:

import java.util.ArrayList;
import lombok.val;
val arrayListExample = new ArrayList();

import java.util.HashMap;
import lombok.val;
val mapExample = new HashMap();

@NonNull

You can use @NonNull annotation on the parameter of constructor or a method to generate null check.
Example:

import lombok.NonNull;
public class LombokExample {
private String name;

public LombokExample (@NonNull Employee employee) {
this.name = employee.getName();
}
}

@Getter and @Setter

We can use @Getter and @Setter to generate getters and setters automatically.
Example:

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class LombokExample {

@Setter(AccessLevel.PROTECTED) private String name;

}

@NoArgsConstructor,  @AllArgsConstructor

@NoArgsConstructor – generate a constructor with no parameters for every variable that require some handling.
@AllArgsConstructor – generates constructor with all variables in class.

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;

@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class LombokExample {

@NonNull private String name;

}
---------------------------------------------
@NoArgsConstructor
public class LombokExample {
@NonNull private String name;
}

@Data
@Data is the simplest way to add following parameters

  • @ToString
  • @EqualsAndHashCode
  • @Getter/ @Setter
  • @RequiredArgsConstructor

@Value
@Value introduced from lombok v0.12.0 to the main lombok kit.

@Value is shorthand for final @ToString @EqualsAndHashCode @AllArgsConstructor

@FieldDefaults(makeFinal = true level = AccessLevel. PRIVATE) @Getter, Except that explicitly including the implementation of the relevant methods basically means that part will not be generated and no warning will be issued.

@Value is that the immutable variant of @Data all fields are made private and final by default, and setters aren’t generated. The class itself is additionally made final by default, because immutability isn’t something which will be forced into a subclass. Like @Data, useful toString(), equals() and hashCode() methods are generated, each field gets a getter method, and a constructor that covers every argument (except final fields that are initialized within the field declaration) is additionally generated.

@Log
You’ll be able to write @Log option on your class (whichever one corresponds to the logging method you’re using). you then have a final static log field initialized as is that the commonly prescribed way of logging the framework you’re using, which you’ll be able to then used to write down log statements.

NEW in lombok v1.16.24: Addition of google’s FluentLogger (via @Flogger).
NEW in lombok v1.18.10: Addition of @CustomLog which allows you to feature any logger by configuring the way to create them with a config key.

Variety of options are available, we can instead start writing log statements in a java class that’s annotated with the @Log annotations from lombok. Lombok supports following spring boot log annotations:

@CommonsLog:

private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LombokExample.class);

@Flogger:

private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();

@JBossLog:

private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LombokExample.class);

@Log:

private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LombokExample.class.getName());

@Log4j:

private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LombokExample.class);

@Log4j2:

private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LombokExample.class);

@Slf4j:

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LombokExample.class);

@XSlf4j:

private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LombokExample.class);

@CustomLog:

private static final com.foo.your.Logger log = com.foo.your.LoggerFactory.createYourLogger(LombokExample.class);

This selection requires that you simply add a configuration to your lombok.config file to specify what @CustomLog should do.
One in all the instance of log annotation as follows, which can be utilized in spring boot application:

You wish to feature below dependency in pom.xml to use lombok annotation

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>

Refer some spring Annotation Click here

 

%d bloggers like this: