Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of RequiredAnnotationBeanPostProcessor in Spring framework?

Tags:

spring

I am a beginner in Spring framework. I have started learning Spring framework a couple of weeks. I did not get any proper explanation of RequiredAnnotationBeanPostProcessor. Please, someone, help me by giving some example of RequiredAnnotationBeanPostProcessor and where to use this. Thanks in advance.

like image 348
emon Avatar asked Mar 04 '18 16:03

emon


People also ask

What is the use of @required annotation?

The @Required annotation applies to bean property setter methods and it indicates that the affected bean property must be populated in XML configuration file at configuration time. Otherwise, the container throws a BeanInitializationException exception.

What is the use of @required annotation in spring?

The @Required annotation in spring is a method-level annotation used in the setter method of a bean property and therefore making the setter-injection compulsory.

What is @configuration annotation in spring?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

Why do we use @autowired annotation?

Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.


1 Answers

RequiredAnnotationBeanPostProcessor is a not common used annotation in applications that use Spring.
The @Autowired annotation that provides both the autowiring and the requiring (by default enabled) behaviors is often preferred to.


RequiredAnnotationBeanPostProcessor is a BeanPostProcessor implementation.

The BeanPostProcessor interface defines callback methods that you can implement to provide your own (or override the container’s default) instantiation logic, dependency-resolution logic, and so forth.

In the case of RequiredAnnotationBeanPostProcessor, it enforces required JavaBean properties to have been configured.
Required bean properties are detected through a Java 5 annotation: by default, the Spring's Required annotation.

To be short, it allows to ensure that a bean that declares 'required' properties has actually been configured with values. Note that the value may be null.

For example suppose this model class :

public class Foo {

    private Bar bar;

     @Required
     public void setBar(Bar bar) {
        this.bar = bar;
    }
}

If setBar() is never invoked during the initialization of the bean, a org.springframework.beans.factory.BeanInitializationException is thrown.

For example this bean configuration will trigger the exception throwing :

@Configuration
public class MyConfig {

    @Bean
    public Foo getFoo() {
        return new Foo();
    }
}

Of course if you add @Autowired to setBar() with a resolvable dependency, it will be fine :

public class Foo {

    private Bar bar;

    @Autowired
    @Required
    public void setBar(Bar bar) {
        this.bar = bar;
    }
}

So we could consider that a good use case for RequiredAnnotationBeanPostProcessor is a case where you don't want/cannot specify the autowiring in the class of the bean.

Note also that RequiredAnnotationBeanPostProcessor provides also an additional feature that is according to the javadoc its primary goal :

The motivation for the existence of this BeanPostProcessor is to allow developers to annotate the setter properties of their own classes with an arbitrary JDK 1.5 annotation to indicate that the container must check for the configuration of a dependency injected value.

It means that you may specify another annotation that @Required to indicate the required constraint.
RequiredAnnotationBeanPostProcessor defines indeed a setRequiredAnnotationType() method that you can override to set the annotation to use.


As you can see, the use of RequiredAnnotationBeanPostProcessor is related to very specific corner cases. That's why you probably don't find many examples about it.

like image 87
davidxxx Avatar answered Oct 13 '22 07:10

davidxxx