Bean post-processors, a special kind of spring bean that able to received bean lifecycle callback at various stages (initialisation/destruction), and perform additional task on each bean before it’s initialisation. Example, RequiredAnnotationBeanPostProcessor is a spring bean post processor, it responsibility is to check if all the bean with it’s properties have been set with @Required annotation.
This example show if annotation @Required is set on the bean properties someText, after component scan, before initialization, spring found the SampleBean it’s property someText has not been set and throws the BeanCreationException and BeanInitializationException. Sometime @Required annotation can useful to ensure your bean must has value needed to be configure properly.
package com.codeomitted.model; import org.springframework.beans.factory.annotation.Required; import org.springframework.stereotype.Component; @Component public class SampleBean { private String someText; public SampleBean() { System.out.println("SampleBean created"); } public String getSomeText() { return someText; } @Required public void setSomeText(String someText) { this.someText = someText; } }
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...... .... // code omitted <context:annotation-config /> <context:component-scan base-package="com.codeomitted.model" /> </beans>
App.java
public class App { public static void main( String[] args ) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:/applicationContext.xml"); ctx.refresh(); ctx.close(); } }
SampleOutput:
Sample bean created
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sampleBean’ defined in file [/Users/mingch/Documents/workspace/worktools/target/classes/com/codeomitted/model/SampleBean.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property ‘someText’ is required for bean ‘sampleBean’
………
Caused by: org.springframework.beans.factory.BeanInitializationException: Property ‘someText’ is required for bean ‘sampleBean’
To fix the issues as mention above, just need to make sure there is value preconfigure for someText property.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...... .... // code omitted <context:annotation-config /> <context:component-scan base-package="com.codeomitted.model" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties"> <value> foo.someText=Hello World bar.someStrin=Such a great day! </value> </property> </bean> </beans>
Modified SampleBean
@Component public class SampleBean { private String someText; public SampleBean() { System.out.println("Sample bean created"); } public String getSomeText() { return someText; } @Required @Value("${foo.someText}") public void setSomeText(String someText) { this.someText = someText; } }