Close

Spring Framework - @Required Examples

Spring Framework 

This example demonstrates how to use @Required annotation with JavaConfig. By default this annotation doesn't work with JavaConfig. As shown in the code, we have to register RequiredAnnotationBeanPostProcessor with Spring container and customize shouldSkip() method. We are not providing ServiceBean to demonstrate how BeanInitializationException is thrown. This is not recommended way to use @Required annotation. Better use @Autowired or implement @PostConstruct to do assertion for required field injected via setters.

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RequiredJConfigExample {

@Bean(autowire = Autowire.BY_TYPE)
public ClientBean clientBean () {
return new ClientBean();
}

// remove this comment to fix BeanInitializationException
/* @Bean
public ServiceBean serviceBean () {
return new ServiceBean();
}*/


@Bean
public RequiredAnnotationBeanPostProcessor processor () {
return new RequiredAnnotationBeanPostProcessor() {
@Override
protected boolean shouldSkip (ConfigurableListableBeanFactory beanFactory,
String beanName) {
if (beanName.equals("clientBean")) {
return false;
}
return super.shouldSkip(beanFactory, beanName);
}
};
}

public static void main (String... strings) {

AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
RequiredJConfigExample.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.work();

}

public static class ClientBean {
private ServiceBean serviceBean;

@Required
public void setServiceBean (ServiceBean serviceBean) {
this.serviceBean = serviceBean;
}

public void work () {
System.out.println("service bean instance: " + serviceBean);
}
}

public static class ServiceBean {
}
}

Output

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientBean' defined in com.logicbig.example.RequiredJConfigExample: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'serviceBean' is required for bean 'clientBean'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.logicbig.example.RequiredJConfigExample.main(RequiredJConfigExample.java:61)
... 6 more
Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'serviceBean' is required for bean 'clientBean'
at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
... 16 more
Original Post




See Also