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

// Create class
@ConfigurationProperties("spring.userConfig")    
@Component
public class UserConfig {
    private String username;
    private String password;
    
    public UserConfig(){
        return new UserConfig();
    }
}

//application.properties
spring.userConfig.username=name
spring.userConfig.password=pass

@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

// Create class
@RequiredArgsConstructor
public class User{
    @NonNull
    private String username;
    @NonNull
    private String password;
    private String email;
    
    
    // This is constructor that will be generated 
    public User(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)
public class User{
    private String username;
    private String pass;
}

//Can create object with this strategy
new User().setUsername("user").setPass("pass");  

@AllArgsConstructor

This annotaion will be declared on the class for generate constructor with all attributes.

Example

// Create class
@Data
@AllArgsConstructor
public class User{
    private String username;
    private String pass;
    private String email
    
    // This is constructor that will be generated 
    public User(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 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

// popular
@Transactional(noRollbackFor = { SQLException.class })
public void createCourseDeclarativeWithNoRollBack(Course course) throws SQLException {
    courseDao.create(course);
    throw new SQLException("Throwing exception for demoing rollback");
}

@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

//Create class
@Data
public class User{
    private String username;
    @JsonIgnore
    private String pass;
    private String 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"})
public class User{
    private String username;
    private String pass;
    private String email;
}

// The response of endpoint that use User class
{
    "username": "user"
}

Last updated