Interpark - Lombok in Intellij

Config Default Spring Project.

If you can’t config Spring Project then please read the article before following this.

Add the dependencies at the build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

// AS-IS

plugins {
id 'java'
id 'war'
}

apply plugin: 'war'

group 'com.kidongyun'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile 'org.springframework:spring-webmvc:4.3.18.RELEASE'
runtime 'javax.servlet:jstl:1.1.2'
}

Let’s add the new dependencies for the lombok. The above thing is the before adding and The below is the after.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// TO-BE

plugins {
id 'java'
id 'war'
}

apply plugin: 'war'

group 'com.kidongyun'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile 'org.springframework:spring-webmvc:4.3.18.RELEASE'
runtime 'javax.servlet:jstl:1.1.2'

compileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
}

Installing the Lombok plugin.

1
File > Settings > Search 'plugin' > Search 'lombok'

Let’s install the Lombok plugin.

Let’s set ‘Enable annotation processing’.

1
File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors > Check the 'Enable annotation processing'.

Let’s make new VO file.

1
2
3
4
5
6
7
8
9
10
11
12

package com.kidongyun.vo;

import lombok.Data;

@Data
public class PersonVO {
String name;
int age;
String major;
}

Let’s re-config the Controller file.

Check whether the lombok configuration is right or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

package com.kidongyun.controller;

import com.kidongyun.vo.PersonVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
@RequestMapping(value = "/")
public String hello(Model model){

PersonVO personVO = new PersonVO();

personVO.setName("Mark");
personVO.setAge(20);s
personVO.setMajor("Computer Engineering");

System.out.println(personVO.getName());
System.out.println(personVO.getAge());
System.out.println(personVO.getMajor());

System.out.println(personVO.toString());

model.addAttribute("msg", "Hello World!");
return "index";
}
}

When you want to use the builder pattern.

You are able to use the builder pattern like the below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

package com.kidongyun.vo;

import lombok.Data;

@Getter
@Setter
@ToString
@Accessors(chain = true)
public class PersonVO {
String name;
int age;
String major;
}

@Builder

You can use the builder pattern style coding only with this annotation. This annotaion can be located at the above of class name. but your domain class should have the super class, It might be not worked well. So The below coding style more better although It’s more longer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

@Getter
@Setter
@ToString
@Entity
@Table(name = "OBJECTIVE")
public class Objective extends Base {
@Column(name = "OBJ_TITLE")
private String title;

@Column(name = "OBJ_DESCRIPTION")
private String description;

@Column(name = "OBJ_DEADLINE")
private LocalDate deadline;

@Column(name = "OBJ_PRIORITY")
private int priority;

@Column(name = "OBJ_STATUS")
private int status;

@Builder
public Objective(String type, long id, String title, String description, LocalDate deadline, int priority, int status) {
super.type = type;
super.id = id;
this.title = title;
this.description = description;
this.deadline = deadline;
this.priority = priority;
this.status = status;
}
}

Share