gpt4 book ai didi

java - Spring Autowire 原始 boolean 值

转载 作者:行者123 更新时间:2023-11-29 03:36:50 26 4
gpt4 key购买 nike

我的 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>

表示VolunteerFormAction是AbstactFormAction的具体实现,会继承AbstactFormAction的属性。

在 AbstractFormAction 中,我这样声明属性:

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

当我尝试部署时出现以下异常:

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)}

好像是在提示找不到byte boolean的bean。但是,当按值而不是按引用定义属性“关闭”时,为什么它需要一个 bean?

最佳答案

您需要使用 @Value使用属性占位符传递值的注释。 @Autowire 期望指定类型的 bean 出现在 applicationContext 中。

如果您正在 Autowiring 值,为什么要将值传递给 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>

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

如果可以使用component-scan你甚至不需要指定创建 beans

您可以添加 <context:component-scan base-package="<your base package>"/>到您的 context.xml 文件并添加注释 @Controller到你的 Controller 文件

关于java - Spring Autowire 原始 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com