I have a project that has a DispatchServlet with a few Controllers. Whenever I tried to use the Required annotation in the controllers it wouldn't work. I would get an ERROR:
Yet if I removed the @Required annotation the property was set fine.
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializati onException: Property 'myProperty' is required for bean 'myController'
After some hunting I finally found the solution:
In our project we have an applicationContext.xml with:
We expected this to just initialize beans with our Module annotation. However, by default Spring initializes Controllers and other things too. So the Controllers were being required prematurely and then Required was being checked.
<context:include-filter type="annotation" expression="edu.calpoly.its.formdriver.plugin.Module"/>
This is documented is this chapter: 3.10.3 Using filters to customize scanning
So make sure to set use-default-filters="false" if you don't want component-scan to pickup your Controllers.
You can also disable the default filters by providing use-default-filters="false" as an attribute of the <component-scan/> element. This will in effect disable automatic detection of classes annotated with @Component, @Repository, @Service, or @Controller.