我有我的applicationContext.xml
<context:property-placeholder location="classpath*:*.properties" />
<bean id="clientPreferencesManager" class="pl.bildpresse.bildchat2.business.ClientPreferencesManager" >
<property name="clientApiUrl" value="${clientapi.url}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
是否有可能通过autowire做同样的事情?就像是 :
@Autowired
@Qualifier("${clientapi.url}")
public void setClientApiUrl(String clientApiUrl) {
this.clientApiUrl = clientApiUrl;
}
Run Code Online (Sandbox Code Playgroud) 我的 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>
Run Code Online (Sandbox Code Playgroud)
表示VolunteerFormAction是AbstactFormAction的具体实现,会继承AbstactFormAction的属性。
在 AbstractFormAction 中,我声明了这样的属性:
@Autowired protected VolunteerSaver volunteerSaver;
@Autowired protected EmailSender emailSender;
@Autowired protected boolean closed;
Run Code Online (Sandbox Code Playgroud)
尝试部署时出现以下异常:
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)}
它似乎在抱怨它找不到字节布尔值的 bean。但是,当定义了按值而不是按引用“关闭”的属性时,为什么它需要一个 bean?