java Spring Autowire 原始布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15042006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Autowire primitive boolean
提问by NickJ
My XML configuration includes these bean definitions:
我的 XML 配置包括这些 bean 定义:
<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.
表示VolunteerFormAction是AbstactFormAction的具体实现,会继承AbstactFormAction的属性。
In AbstractFormAction, I declare the properties like this:
在 AbstractFormAction 中,我声明了这样的属性:
@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)}
org.springframework.beans.factory.BeanCreationException:创建名为“volunteerFormAction”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:受保护的布尔值 staffing.server.action.form.AbstractFormAction.closed; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有为依赖找到 [boolean] 类型的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@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?
它似乎在抱怨它找不到字节布尔值的 bean。但是,当定义了按值而不是按引用“关闭”的属性时,为什么它需要一个 bean?
回答by Arun P Johny
You need to use @Valueannotation for passing values using property place holders. @Autowire expects a bean of the specified type to be present in the applicationContext.
您需要使用@Value注释来使用属性占位符传递值。@Autowire 期望 applicationContext 中存在指定类型的 bean。
If you are autowiring the values why are you passing the values int he bean definition? I think what you need is
如果您要自动装配值,为什么要在 bean 定义中传递值?我想你需要的是
<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-scanyou need not even specify create the beans
如果您可以使用组件扫描,您甚至不需要指定创建 bean
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
您可以添加<context:component-scan base-package="<your base package>"/>
到您的 context.xml 文件并将注释添加@Controller
到您的控制器文件
回答by zagyi
You shouldn't annotate closed
with @Autowired
.
你不应该closed
用@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]"
@Autowired
指示 Spring 在您的上下文中查找自动装配字段 (boolean) 类型的 bean,这就是它抱怨“没有匹配的 [boolean] 类型的 bean”的原因
If you inject the value from xml config, there is no need for any annotation on that field.
如果您从 xml config 注入值,则该字段上不需要任何注释。
回答by Ryan Stewart
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 supposedto live. That means there are twoinstances 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:
根据您显示的代码,您加载 Spring 上下文的方式可能存在问题。我的猜测是,您在根 Web 应用程序上下文和控制器应该存在的子上下文中都错误地对控制器进行了组件扫描。这意味着要创建这个类的两个实例,并且只有其中一个是通过 XML 配置的。Spring 正在尝试自动装配另一个实例,但由于给定的错误而失败。您会在其他几个 SO 答案中找到问题和解决方案的描述,如下所示:
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.
如果您提供有关配置文件和上下文配置的更多详细信息,有人可能会准确指出您出错的地方。