Table of contents
This tutorial is part of Springing into Action: A Spring Boot Journey from Novice to Pro Series, be sure to check it out for more related content!
Spring Boot and Lombok are two popular frameworks used in Java development. They both provide a set of annotations that can make your code more concise and readable. In this article, we will take a deeper look at some of the most commonly used annotations in Spring Boot and Lombok, their uses and how they can help you write better code.
Spring Boot Annotations
@SpringBootApplication
: This is a convenience annotation that is equivalent to including the@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
annotations. It is used to enable auto-configuration and component scanning in your application.@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@RestController
: This annotation is used to create a RESTful controller in Spring Boot. It is a combination of the@Controller
and@ResponseBody
annotations.@RestController public class MyController { @GetMapping("/") public String home() { return "Hello, World!"; } }
@RestControllerAdvice
: This annotation is used to handle the global exception handling in the application.@RestControllerAdvice public class ExceptionAdvice { @ExceptionHandler(value = Exception.class) public ResponseEntity<Object> handleException(Exception ex) { return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } }
@Autowired
: This annotation is used to inject a dependency into a class. It can be used on fields, methods, or constructors.@Service public class MyService { @Autowired private MyRepository myRepository; }
@Value
: This annotation is used to assign a value to a field from a properties file.@Value("${app.name}") private String appName;
@PostConstruct
: This annotation is used to indicate a method to be executed after the bean is created and all the dependencies are injected.@Service public class MyService { @Autowired private MyRepository myRepository; @PostConstruct public void init() { //some initialization code } }
Lombok Annotations
@Data
: This annotation generates getters and setters,toString
,equals
, andhashCode
methods for a class.@Data public class Student { private Long id; private String name; }
@NoArgsConstructor
: This annotation generates a constructor with no arguments.@NoArgsConstructor public class Student { private Long id; private String name; }
@AllArgsConstructor
: This annotation generates a constructor with all fields as arguments.@AllArgsConstructor public class Student { private Long id; private String name; }
@Builder
: This annotation generates a builder pattern for a class@Builder public class Student { private Long id; private String name; }
@Slf4j
: This annotation is used to generate a logger for the class@Slf4j public class MyService { public void myMethod() { log.info("This is an example of log"); } }
These are just a few examples of the annotations provided by Spring Boot and Lombok. Using these annotations can help you write cleaner and more readable code, making it easier to maintain and understand.
In conclusion, Spring Boot and Lombok are powerful tools that can help you to write better code and improve your productivity as a developer. With the help of annotations, you can make your code more readable and maintainable. I hope this article has been helpful in introducing some of the most useful annotations provided by these frameworks.