简体   繁体   English

弹簧自动接线

[英]Spring autowiring

I have a Spring 3.0 project that I'm trying to wire up that has a dependency on a library project (also Spring 3.0) which has several classes that have properties being injected via org.springframework.beans.factory.annotation.Value . 我正在尝试连接一个Spring 3.0项目,该项目依赖于一个库项目(也就是Spring 3.0),该项目具有几个类,这些类具有通过org.springframework.beans.factory.annotation.Value注入的属性。

I do not need the classes with injected properties to be loaded nor the properties to be injected. 我既不需要加载具有注入属性的类,也不需要注入属性。 I only need one specific class to be autowired from the library project. 我只需要从库项目中自动连接一个特定的类。

I keep getting the following exception: 我不断收到以下异常:

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

Here is the snippet of my applicationContext.xml. 这是我的applicationContext.xml的片段。 I have tried several versions of the following but the exclusions/inclusion filters do not appear to work. 我尝试了以下几种版本,但排除/包含过滤器似乎不起作用。

<context:component-scan base-package="com.example.library" >
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>     
</context:component-scan>

I am using a PropertyPlaceholderConfigurer as suggested by one of the answers, this already existed in my project. 我使用的是答案之一建议的PropertyPlaceholderConfigurer,这已存在于我的项目中。 Also, I have removed all of the component-scan and annotation-config from my Spring config files. 另外,我已经从我的Spring配置文件中删除了所有component-scan和注解配置。

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

I should add that this error occurs while running a unit test that extends a super class with the following annotations: 我应该补充一点,在运行使用以下注释扩展超类的单元测试时,会发生此错误:

@TransactionConfiguration(defaultRollback = true,transactionManager="txManager")
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicationContext.xml")
public abstract class BaseTest {

If you need a single component from that library, then it's probably better to define it explicitly. 如果您需要该库中的单个组件,则最好显式定义它。 For example: 例如:

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

... or using Java-based container configuration . ...或使用基于Java的容器配置

If you actually need to scan this package, then make sure that all possible cases are handled properly by your exclusion filters. 如果您确实需要扫描此软件包,请确保排除筛选器正确处理了所有可能的情况。 The stack trace suggests, that the component is annotated with @Controller, not @Service. 堆栈跟踪表明,该组件使用@Controller而不是@Service进行了注释。 However, there are other options, that should be considered as well, like @Component and @Repository. 但是,还有其他选项也应考虑,例如@Component和@Repository。

Turns out there was a very simple solution that I overlooked. 事实证明,有一个非常简单的解决方案被我忽略了。 My library project jar was including its applicationContext.xml and I didn't realize it. 我的图书馆项目jar包含其applicationContext.xml,但我没有意识到。 I removed that, rebuilt the jar and its working properly. 我删除了它,重建了罐子并使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM