Spring - @Required annotation example

Posted on January 31, 2017


Technologies used:   JDK 1.8.0_121 | Spring 4.3.5.RELEASE | Maven 3.3.9 | Eclipse Mars.2 (4.5.2)

The @Required annotation is method-level annotation and applied to the setter method of a bean.

This annotation simply indicates that the setter method must be configured to be dependency-injected with a value at configuration time.

In this post, we will show you how the @Required annotation works in spring application. 

Consider the following beans - BeanOne and BeanTwo.

BeanOne.java

package com.boraji.tutorial.spring;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;

@Component
public class BeanOne {
   
   private BeanTwo beanTwo;

   @Required
   public void setBeanTwo(BeanTwo beanTwo) {
      this.beanTwo = beanTwo;
   }
   
   public void doSomething(){
      System.out.println("Inside do doSomething() method of BeanOne");
      beanTwo.doSomething();
   }
}

BeanTwo.java

package com.boraji.tutorial.spring;

import org.springframework.stereotype.Component;

@Component
public class BeanTwo {
   public void doSomething() {
      System.out.println("Inside do doSomething() method of BeanTwo");
   }
}

Create a java based configuration class named as AppConfig and write the following code in it.

AppConfig.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.boraji.tutorial.spring")
public class AppConfig {

}

@ComponentScan annotation scans all beans, whose class is annotated by the @Component annotation in a package, specified by basePackages attribute.

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = 
            new AnnotationConfigApplicationContext(AppConfig.class);
      
      BeanOne beanOne = context.getBean(BeanOne.class);
      beanOne.doSomething();
      
      context.close();
   }
}

Output

Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'beanTwo' is required for bean 'beanOne'
	at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:155)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1225)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552)
	... 10 more

It is clear from the above exception that the setter method, annotated with @Required annotation, must be populated with a value.

 

Now modify the BeanOne as follows.

@Component
public class BeanOne {
   
   private BeanTwo beanTwo;

   @Required
   @Autowired
   public void setBeanTwo(BeanTwo beanTwo) {
      this.beanTwo = beanTwo;
   }
   
   // .....
}

Run main class and see output.

Output

Inside do doSomething() method of BeanOne
Inside do doSomething() method of BeanTwo