2013-03-22 112 views
0

我有一个春天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 { 
+0

如果你告诉我哪个图书馆可能可以帮忙。错误是非常奇怪的 – emd 2013-03-22 22:32:55

+3

可能是更好的选项来声明你需要的bean而不是扫描 – 2013-03-22 23:02:19

+0

你的配置似乎没有错,但也许你失败的类'com.example.library.controller.App'建议应用程序类用'@ Controller'注释,而不是'@Service',所以你可能需要在'中改变表达式。'无论如何,我会用一个属性源来解决这个问题。 “app.achDir”的“不关心”值,因为你不用它(和bean) – 2013-03-22 23:40:31

回答

0

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

1

如果您需要从库中的一个组成部分,那么它可能会更好明确地定义它。例如:

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

...或使用Java-based container configuration

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

+0

问题是,即使当我不做组件扫描,我仍然得到错误。 – 2013-03-25 00:51:06

+0

错误消息是否完全相同? 1)确保组件不被其他组件扫描(例如,作为应用程序中的“常规”组件)2)尝试仅为该组件设置这些属性(使用[PropertyPlaceholderConfigurer](http:// static .springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html))并查看是否有任何更改。 – 2013-03-25 06:49:21

+0

是的,完全相同的错误。该项目使用PropertyPlaceholderConfigurer。 – 2013-03-25 13:56:26