Spring Autowire primitive boolean

我是研究僧i 提交于 2019-12-11 11:42:42

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/15042006/spring-autowire-primitive-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!