Java 弹簧自动接线

Java 弹簧自动接线,java,spring,Java,Spring,我有一个Spring3.0项目,我正在尝试连接它,它依赖于一个库项目(也是Spring3.0),它有几个类,这些类的属性通过org.springframework.beans.factory.annotation.Value注入 我不需要加载具有注入属性的类,也不需要注入属性。我只需要从库项目自动连接一个特定类 我一直得到以下例外情况: Caused by: org.springframework.beans.factory.BeanCreationException: Could not au

我有一个Spring3.0项目,我正在尝试连接它,它依赖于一个库项目(也是Spring3.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>

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

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

。。。或使用


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

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

如果你告诉我哪个库可能我可以帮忙。这个错误真的很奇怪,可能是声明您所需的bean而不是scanNothing的更好选择。您的配置似乎没有任何问题,但是您失败的类
com.example.library.controller.App
表明应用类是用
@controller
注释的,而不是
@Service
,因此,您可能必须更改
中的表达式,如何编写
@值
?您是否有
PropertyPlaceHolderConfigure
?@Jose,我明天会尝试,但我确信即使我不进行扫描,也会收到错误消息。问题是,即使我不进行组件扫描,也会收到错误消息。错误消息是否完全相同?1) 确保该组件未被其他组件扫描(例如,作为应用程序中的“常规”组件)拾取2)尝试仅为此组件设置这些属性(使用),然后查看是否有任何更改。是的,完全相同的错误。项目正在使用PropertyPlaceHolderConfigure。如果从项目中删除所有扫描并使用简单声明创建组件,错误消息是否仍然相同?你也可以发布这个片段吗?在一个队友将ContextConfiguration属性更改为引用其他位置后,我终于解决了这个问题。当这是一个类路径问题时。。。。我的库包含了applicationContext.xml文件,但我没有意识到,这导致组件仍然被扫描。移除了它,工作正常。
@TransactionConfiguration(defaultRollback = true,transactionManager="txManager")
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicationContext.xml")
public abstract class BaseTest {
<bean id="..." class="com.example.library.SomeComponent">
 <!-- Bean initialization -->
</bean>