我在 Spring 3.2.2 项目中有以下 bean 类:
@Service
public class PropertyFacade {
// This DOES NOT work
@Autowired
private String propertyFileName;
// This DOES work
// @Autowired
// public void setPropertyFileName( String propertyFileName ) {
// this.propertyFileName = propertyFileName;
// }
我的弹簧配置文件有以下内容:
<context:component-scan base-package="org.sperbolink.utility" />
<context:component-scan base-package="org.sperbolink.facade" />
<bean id="propertyFacade" class="org.sperbolink.facade.PropertyFacade" >
<property name="propertyFileName" value="test-properties.inf" />
</bean>
我的调用方法如下所示:
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext( "spring-config-test.xml" );
BeanFactory factory = classPathXmlApplicationContext;
PropertyFacade propertyFacade = ( PropertyFacade ) factory.getBean( "propertyFacade" );
出于某种原因,当我尝试自动装配属性时,出现错误:
springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String org.sperbolink.facade.PropertyFacade.propertyFileName; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
但是,当我自动装配设置器时,一切正常。为什么属性自动装配不起作用?