1

My XML configuration includes these bean definitions:

<bean id="abstractFormAction" class="staffing.server.action.form.AbstractFormAction" abstract="true" parent="baseAction">
    <property name="volunteerSaver" ref="volunteerSaver"/>
    <property name="emailSender" ref="emailSender"/> 
    <property name="closed" value="${form.closed}"/>
</bean>

<bean id="volunteerFormAction" class="staffing.server.action.form.VolunteerFormAction" parent="abstractFormAction">
    <property name="captchaGenerator" ref="captcha"/>
</bean>

Indicating that VolunteerFormAction is a concrete implementation of AbstactFormAction, and will inherit the properties of AbstactFormAction.

In AbstractFormAction, I declare the properties like this:

@Autowired protected VolunteerSaver volunteerSaver;
@Autowired protected EmailSender emailSender;
@Autowired protected boolean closed;

I get the following exception when I try to deploy:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'volunteerFormAction': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected boolean staffing.server.action.form.AbstractFormAction.closed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [boolean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

It seems to be complaining that it cannot find a bean of byte boolean. But why would it want a bean when have defined property 'closed' by value, not by reference?

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
NickJ
  • 9,380
  • 9
  • 51
  • 74

3 Answers3

5

You need to use @Value annotation for passing values using property place holders. @Autowire expects a bean of the specified type to be present in the applicationContext.

If you are autowiring the values why are you passing the values int he bean definition? I think what you need is

<bean id="abstractFormAction" class="staffing.server.action.form.AbstractFormAction" abstract="true" parent="baseAction"><bean>
<bean id="volunteerFormAction" class="staffing.server.action.form.VolunteerFormAction" parent="abstractFormAction">
    <property name="captchaGenerator" ref="captcha"/>
</bean>

and

@Autowired protected VolunteerSaver volunteerSaver;
@Autowired protected EmailSender emailSender;
@Value("#{form.closed}") protected boolean closed;

If you can use component-scan you need not even specify create the beans

You can add <context:component-scan base-package="<your base package>"/> to your context.xml file and add the annotation @Controller to your controller file

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • If the context is set up properly, fully defining the bean in XML won't give the error indicated because Spring will see that a value has already been provided for the field. – Ryan Stewart Feb 23 '13 at 15:58
0

You shouldn't annotate closed with @Autowired.

@Autowired instructs Spring to look up a bean of the type of the autowired field (boolean) in your context, that's why it's complaining about "No matching bean of type [boolean]"

If you inject the value from xml config, there is no need for any annotation on that field.

zagyi
  • 17,223
  • 4
  • 51
  • 48
0

Based on the code you've shown, it's likely that you have a problem in the way you're loading your Spring contexts. My guess is that you're incorrectly component-scanning your controllers in both the root web application context and in the child context where the controllers are supposed to live. That means there are two instances of this class being created, and only one of them is being configured via the XML. Spring is attempting to autowire the other instance and failing with the given error. You'll find descriptions of the problem and solution in several other SO answers, like these:

Declaring Spring Bean in Parent Context vs Child Context

Spring XML file configuration hierarchy help/explanation

Spring-MVC: What are a "context" and "namespace"?

If you give more detail about your config files and context configuration, someone might be able to point out exactly where you're going wrong.

Community
  • 1
  • 1
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199