Spring Boot Annotaion
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
@Bean
public RestTemplate createRestTemplate(){
return new RestTemplate(1000,"rest1");
}
@Bean
@Primary
public RestTemplate createRestTemplate2(){
return new RestTemplate(2000,"rest2");
}
//This service will create resttemplate2
public class Service {
@Autowired
private RestTemplate rest2;
}@ConfigurationProperties
This annotaion will be declared on the class or method and use for load configuration from application.properties.
Example
@Builder
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
@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.
@AllArgsConstructor
This annotaion will be declared on the class for generate constructor with all attributes.
Example
@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 method and 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.
Example
@JsonIgnore
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
@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
Last updated