我有一个 Spring 3.0 项目,我正在尝试连接它,它依赖于一个库项目(也是 Spring 3.0),该项目有几个类,这些类具有通过org.springframework.beans.factory.annotation.Value注入的属性。
我不需要加载具有注入属性的类,也不需要注入属性。我只需要从库项目中自动装配一个特定的类。
我不断收到以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.example.library.controller.App.dir; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'app.achDir'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
... 38 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.achDir'
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:403)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:736)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:713)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 40 more
这是我的 applicationContext.xml 的片段。我已经尝试了以下几个版本,但排除/包含过滤器似乎不起作用。
<context:component-scan base-package="com.example.library" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
我正在使用答案之一所建议的 PropertyPlaceholderConfigurer,这已经存在于我的项目中。另外,我已经从我的 Spring 配置文件中删除了所有的组件扫描和注释配置。
<!-- Define the other the old-fashioned way, with 'ignoreUnresolvablePlaceholders' set to TRUE -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>src/test/resources/my.properties</value>
<value>my.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
我应该补充一点,运行单元测试时会发生此错误,该单元测试使用以下注释扩展超类:
@TransactionConfiguration(defaultRollback = true,transactionManager="txManager")
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicationContext.xml")
public abstract class BaseTest {