Describe about usability for each annotaion in spring boot
@Bean
This annotaion will be declared on the method inside the class that has @Configuration for create a object or bean.
@Configuration
This annotaion will be declared on the class and when application start , spring will look for @Bean then generate bean (create object) for each method that has @Bean to spring container at runtime.
@Component
This annotaion will be declared on the class and object will be created from this class to spring container.
@Primary
When you have 2 objects with the same class but inside difference , This annotaion will determine which one is the main.
Example
// 2 method for create bean @BeanpublicRestTemplatecreateRestTemplate(){returnnewRestTemplate(1000,"rest1"); } @Bean @PrimarypublicRestTemplatecreateRestTemplate2(){returnnewRestTemplate(2000,"rest2"); }//This service will create resttemplate2 publicclassService { @AutowiredprivateRestTemplate rest2; }
@ConfigurationProperties
This annotaion will be declared on the class or method and use for load configuration from application.properties.
This annotaion will be declared on the class for create a immutable object with builder pattern.
@RequiredArgsConstructor
This annotaion will be declared on the class for generate constructor. It will generate constructor for only attribute that has final or @NonNull.
Example
// Create class@RequiredArgsConstructorpublicclassUser{ @NonNullprivateString username; @NonNullprivateString password;privateString email;// This is constructor that will be generated publicUser(String username,String password){this.username= username;this.password= password; }}
@Data
This annotaion will be declared on the class that including @ToString , @EqualsAndHashCode , @Getter/@Setter and @RequiredArgsConstructor.
@Accessor
This annotation will be declared on the class for configure @Getter/@Setter.
Example
@Accessor(chain = true) it will determine setter method to return this instead void.
// Create class@Data@Accessor(chain =true)publicclassUser{privateString username;privateString pass;}//Can create object with this strategynewUser().setUsername("user").setPass("pass");
@AllArgsConstructor
This annotaion will be declared on the class for generate constructor with all attributes.
Example
// Create class@Data@AllArgsConstructorpublicclassUser{privateString username;privateString pass;privateString email// This is constructor that will be generated publicUser(String username,String password,String email){this.username= username;this.password= password;this.email= email; }}
@NotNull
This annotation will be declared on attribute inside the class for mandatory value (not null value).
@NotEmpty
This annotation will be declared on attribute inside the class that the value mustn't be empty.
@NotBlank
This annotaion will be declared on attribute inside the class that the value mustn't be null and empty.
@Valid
This annotation will be declared with parameter for validation value. It validate by using JSR-303 or javax.validation on methodand can use on attribute for nested object.
@Validated
This annotaion will be similar to @Valid but it supports for validation in group level and declares on the class.
@Transactional
This annotaion will be declared on the class or method for rollback SQL query. It will create proxy for method or class that add this annotaion (Proxy Pattern) , if unchecked exception occur it will rollback SQL query. For handle check exception you can use @Transactional(rollBackFor = "TestException.class" ) or (noRollBackFor = "TestException.class"). Furthermore the class implement interface class or the method that access modifier doesn't public can't use this annotation.
This annotaion will be declared on attribute inside object class to determine this class mustn't return this field in the response of endpoint.
Example
//Create class@DatapublicclassUser{privateString username; @JsonIgnoreprivateString pass;privateString email;}// The response of endpoint that use User class{"username":"user","email":"test@mail.com"}
@JsonIgnoreProperties
This annotation will be declared on the class for manage about ignore properties in JSON so it has much custom feature than @JsonIgnore.
Example
// Create class@Data@JsonIgnoreProperties({"pass","email"})publicclassUser{privateString username;privateString pass;privateString email;}// The response of endpoint that use User class{"username":"user"}
/* UseCase1. Create Service to call request API to another service(A to B).2. Create response object of B in A service.3. Create Controller that call 1. and this endpoint will return response that is the same as B response.4. The problem will occur when service B send the response more field than A service response object.*/// Create class in A service@Data@JsonIgnoreProperties(ignoreUnknown =true)publicclassUser{privateString username;privateString pass;privateString email;}//Create controller in A service (return User)publicUsertestController(){}/* The response from B service *///expected{"username":"user","pass":"pass","email":"test@mail.com"}//actual{"username":"user","pass":"pass","email":"test@mail.com","id":"123, "name": "bob"}/* The response from controller in A service *///Before add annotaion{ "username": "user", "pass": "pass", "email": "test@mail.com", "id": "123,"name":"bob"}//After add annotaion{"username":"user","pass":"pass","email":"test@mail.com"}