Announcement Announcement Module
No announcement yet.
[solved] Required being checked before value set. (Be careful with component-scan) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • [solved] Required being checked before value set. (Be careful with component-scan)

    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:
    Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializati onException: Property 'myProperty' is required for bean 'myController'
    Yet if I removed the @Required annotation the property was set fine.

    After some hunting I finally found the solution:

    In our project we have an applicationContext.xml with:
    <context:component-scan base-package="edu.calpoly">
      <context:include-filter type="annotation" expression="edu.calpoly.its.formdriver.plugin.Module"/>
    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.

    This is documented is this chapter: 3.10.3 Using filters to customize scanning

    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.
    So make sure to set use-default-filters="false" if you don't want component-scan to pickup your Controllers.