The power of Lombok

Hi!

Today I would like to introduce you to Lombok – a very useful tool for Java coders.

What is Lombok?

Shortly speaking, it’s a library that reduces the amount of boilerplate code in your Java projects.聽Let me show you some Lombok power using example with simple Employee class. Employee has three fields along with corresponding getters/setters.

public class Employee { 

private String name; 
private String surname; 
private int age; 

public String getName() { return name; } 
public void setName(String name) { this.name = name; } 
public String getSurname() { return surname; } 
public void setSurname(String surname) { this.surname = surname; } 
public int getAge() { return age; } 
public void setAge(int age) { this.age = age; } 
}

Now see what happens when you are using Lombok:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class EmployeeWithLombok {

private String name; 
private String surname; 
private int age; 
}

Yes, this code above is sufficient – you only need two Lombok annotations to have all your getters and setters available.

And I have once thought that have your getters/setters generated by Eclipse is a cool thing 馃槈

What else can Lombok do for you?

Generates your setters/getters:
@Getter/@Setter

Generates implementation of Object methods:
@ToString
@EqualsAndHashCode

Generates your constructors in different variants:
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor

Generates a builder for your objects:
@Builder

You can find some more at Lombok site:聽https://projectlombok.org/features/all聽

And how to get it

Now I will show how to get Lombok running in your Eclipse:

  1. Go to https://projectlombok.org/download and get newest lombok
  2. Go to Windows cmd and find directory where you download lombok.jar
  3. Type: java – jar lombok.jar. The Lombok window should appear:lombok_installing
  4. Click on聽Specify location..and find directory with your Eclipse.inilombok_installing_find_eclipse_ini
  5. . Then click on聽Install/Update聽and have your Lombok installedlombok_installing_successfull
  6. . Restart yout Eclipse (if needed)
  7. 聽Add Lombok dependency to your Maven pom.xml
    聽<dependencies>
    聽聽聽聽聽聽 <dependency>
    聽聽聽聽聽聽聽聽聽聽聽聽 <groupId>org.projectlombok</groupId>
    聽聽聽聽聽聽聽聽聽聽聽聽 <artifactId>lombok</artifactId>
    聽聽聽聽聽聽聽聽聽聽聽聽 <version>1.16.16</version>
    聽聽聽聽聽聽聽聽聽聽聽聽 <scope>provided</scope>
    聽聽聽聽聽聽 </dependency>
    </dependencies>
  8. Click on Project -> Maven -> Update Project
  9. Enjoy Lombok power! 馃檪
Advertisements

The simplest way to set up your first project for Spring web app

Hi!

Have you ever felt frustrated when trying to configure your first Spring project? You want to start developing web apps with Spring MVC but managing all Maven depencies is slowing you down? Not to mention that you’ll soon need a server to get your app running..

I have also spent some long hours, struggling to set up my Spring project workspace along with Tomcat server. And then, during the traing I have attended, I have learnt about SpringBoot and Spring聽Initializr that makes the whole process easier.

Below you’ll find step by step guide showing how to create your first SpringBoot project using Spring Initializr.

  1. Go to https://start.spring.io/1. Go to Spring Initializr
  2. Choose Maven or Gradle Project
  3. Choose language
  4. Type your application name
  5. Search for needed dependencies (or click on Switch to the full version to see all the details)
  6. When you’re ready, click on generate project2. Generate your projectAs you can see I’ve chosen Web dependency as we want to start a web app. According to Spring Initializr choosing Web dependency gives us “Full-stack web development with Tomcat and Spring MVC”
  7. Now unpack zip folder in your workspace
  8. Import project to your workspace in Eclipse (File -> Import,聽then choose Existing Maven project)3. Import Existing Maven Project
  9. Browse directory for your project4. Browse for your project
  10. Check Add project to working set聽and click on聽Finish5. Finish importing
  11. Wait till workspace is built… and your project is now ready!6. Imported project is ready to useAlong with POM file:7. Pom is generated

I hope Spring Initializr will also save some of your time in future 馃檪

 

Eclipse: opcje, kt贸re u艂atwiaj膮 prac臋 z kodem – cz臋艣膰 III / Eclipse: options that make coding easier – part III

Cze艣膰! Dzi艣 kolejna聽cz臋艣膰 wskaz贸wek, u艂atwiaj膮cych prac臋 z kodem w 艣rodowisku Eclipse. W pierwszej cz臋艣ci pokaza艂am opcje przydatne przy tworzeniu nowych klas: generowanie getter贸w i setter贸w, metod hashCode(), equals() oraz toString(). W drugiej cz臋艣ci by艂a mowa o聽opcjach przydatnych gdy zmieniamy istniej膮cy kod – zmianie nazw zmiennych, zmianie sygnatury metod i tworzeniu sta艂ych. W tej cz臋艣ci chc臋 Wam pokaza膰 jak wygenerowa膰 konstruktory oraz do czego s艂u偶膮 opcje “Extract interface” i “Extract method”.

***

Hi! Today next part of hints, that make coding in Eclipse easier . In the first part, I have showed options useful when you create new class: generating getters and setters, hashCode(),聽equals() and toString() methods. In the聽second part, there were presented options useful while changing the existing code – extracting constant, renaming variables and changing method signature. Now I am going to show you how to generate constructors and how to extract the interface or the method from the code.

Continue reading “Eclipse: opcje, kt贸re u艂atwiaj膮 prac臋 z kodem – cz臋艣膰 III / Eclipse: options that make coding easier – part III”

Eclipse: opcje, kt贸re u艂atwiaj膮 prac臋 z kodem – cz臋艣膰 II / Eclipse: options that make coding easier – part II

Cze艣膰! Dzi艣 druga cz臋艣膰 wskaz贸wek, u艂atwiaj膮cych prac臋 z kodem w 艣rodowisku Eclipse. W poprzedniej cz臋艣ci pokaza艂am opcje przydatne przy tworzeniu nowych klas: generowanie getter贸w i setter贸w, metod hashCode() i equals() oraz metody toString(). W tej cz臋艣ci zaprezentuj臋 Wam opcje, kt贸rych korzystamy w momencie, gdy chcemy co艣 zmieni膰 w ju偶 istniej膮cym kodzie.

***

Hi! Today second part of hints, that make coding in Eclipse easier . In the previous part, I have showed options useful when you create new class: generating getters and setters, hashCode() and equals() methods and toString() method. In this part, I will show you the options, that we use when we want to change something in already existing code.

Continue reading “Eclipse: opcje, kt贸re u艂atwiaj膮 prac臋 z kodem – cz臋艣膰 II / Eclipse: options that make coding easier – part II”

Eclipse: opcje, kt贸re u艂atwiaj膮 prac臋 z kodem – cz臋艣膰 I / Eclipse: options that make coding easier – part I

Cze艣膰! Chcia艂abym聽pokaza膰聽Wam kilka opcji w Eclipsie, kt贸re u艂atwiaj膮 codzienn膮 prac臋 z kodem: oszcz臋dzaj膮 nasz czas i chroni膮 przed przypadkowymi b艂臋dami. Na pocz膮tek trzy opcje przydatne przy tworzeniu nowej klasy. Wi臋kszo艣膰 z Was pewnie od dawna je zna i stosuje (i bardzo dobrze :)), ale mo偶e kto艣 jeszcze聽ich nie odkry艂, a uzna je za pomocne.

***

Hi! I would like to show you a few options in Eclipse, that make everyday coding easier. Using them, saves our time and protects against typos. To begin with, three options useful by creating a new class. I guess most of you use them on a regular basis (and that’s good :)), but maybe someone has not discovered them yet and will consider them handy.

Continue reading “Eclipse: opcje, kt贸re u艂atwiaj膮 prac臋 z kodem – cz臋艣膰 I / Eclipse: options that make coding easier – part I”