How to Disable/Skip @Required Annotation For Class

When you see this title, you may think “why should I skip a required field, can’t I just remove the annotation?”. And you are probably right. But imagine when you are using a third-party library which contains some @Required annotations that are badly placed, you do have the option to disable or skip the @Required annotation. Let’s see how.

Marking setter method with @Required

Suppose we have a class with the @Required annotation. But we want to disable or skip this annotation. We don’t have the source code of this library so we cannot change or remove the annotation.

package com.memorynotfound.spring.core.required;

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

public class StarWars {

    private TheForce theForce;

    public TheForce getTheForce() {
        return theForce;
    }

    @Required
    public void setTheForce(TheForce theForce) {
        this.theForce = theForce;
    }
}

This class is marked as required in the previous one.

package com.memorynotfound.spring.core.required;

public class TheForce {

}

Disable or Skip @Required Dependency Checking for class

To disable or skip the @Required annotation for a class we can configure the bean definition with a meta element with the org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.skipRequiredCheck and assign a value of true. This will skip the required annotation completely for that class.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id="luke" class="com.memorynotfound.spring.core.required.StarWars">
        <meta key="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.skipRequiredCheck" value="true"/>
    </bean>

</beans>

Running the application

Note that the StarWars has a required annotation. We skipped the validation of this class. So when we boot the application without providing a value, we’ll see that the application boots successfully.

package com.memorynotfound.spring.core.required;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String... args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
        StarWars starWars = (StarWars)context.getBean("luke");
        System.out.println(starWars);
    }
}

References

Download

You may also like...