春天自动装配

时间:2013-03-22 22:08:48

标签: java spring

我有一个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配置文件中删除了所有的component-scan和annotation-config。

<!-- 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 {

2 个答案:

答案 0 :(得分:1)

如果您需要该库中的单个组件,那么最好明确定义它。例如:

<bean id="..." class="com.example.library.SomeComponent">
 <!-- Bean initialization -->
</bean>

...或使用Java-based container configuration

如果您确实需要扫描此软件包,请确保排除过滤器正确处理所有可能的案例。堆栈跟踪表明,该组件使用@Controller注释,而不是@Service。但是,还有其他选项,也应该考虑,如@Component和@Repository。

答案 1 :(得分:0)

原来我忽略了一个非常简单的解决方案。我的库项目jar包括它的applicationContext.xml,我没有意识到。我删除了它,重建了jar并且它正常工作。