我有一個春天3.0項目,我試圖連線,有一個庫項目(也是春天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 {
如果你告訴我哪個圖書館可能可以幫忙。錯誤是非常奇怪的 – emd 2013-03-22 22:32:55
可能是更好的選項來聲明你需要的bean而不是掃描 – 2013-03-22 23:02:19
你的配置似乎沒有錯,但也許你失敗的類'com.example.library.controller.App'建議應用程序類用'@ Controller'註釋,而不是'@Service',所以你可能需要在'中改變表達式。'無論如何,我會用一個屬性源來解決這個問題。 「app.achDir」的「不關心」值,因爲你不用它(和bean) –
2013-03-22 23:40:31