【问题标题】:How to use a Spring config file in a Maven dependency如何在 Maven 依赖项中使用 Spring 配置文件
【发布时间】:2010-04-16 08:43:21
【问题描述】:

在依赖项 A 中,我有以下内容:

<beans>
    <bean
        id="simplePersonBase"
        class="com.paml.test.SimplePerson"
        abstract="true">
        <property
            name="firstName"
            value="Joe" />
        <property
            name="lastName"
            value="Smith" />
    </bean>
</beans>

然后在项目 B 中,我将 A 添加为依赖项并具有以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean
        id="simplePersonAddress01"
        parent="simplePersonBase">
        <property
            name="firstName"
            value="BillyBob" />
        <property
            name="address"
            value="1060 W. Addison St" />
        <property
            name="city"
            value="Chicago" />
        <property
            name="state"
            value="IL" />
        <property
            name="zip"
            value="60613" />
    </bean>
</beans>

当我像这样使用 ClassPathXmlApplicationContext 时:

    BeanFactory beanFactory = new ClassPathXmlApplicationContext( new String[] {
        "./*.xml" } );

    SimplePerson person = (SimplePerson)beanFactory.getBean( "simplePersonAddress01" );
    System.out.println( person.getFirstName() );

Spring 抱怨它无法解析父 xml。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'simplePersonBase' is defined

我确信有办法做到这一点,但是我还没有找到。有人知道怎么做吗?

【问题讨论】:

    标签: java spring dependencies


    【解决方案1】:

    尝试使用classpath*: 前缀。

    【讨论】:

    • 我以前看过这个,但另一个错误误导了我。作为记录,这是我更改后的内容: BeanFactory beanFactory = new ClassPathXmlApplicationContext( new String[] { "classpath*:*.xml" } );
    【解决方案2】:

    A.jave有对应的xml文件吗?在项目A中,你是src/main/resources里面的xml吗?

    【讨论】:

    • 是的,第一个xml sn -p在项目A的src/main/resources中。
    【解决方案3】:

    并不是这个问题的真正答案 - 但要注意这种方法。

    解决错误可能是一场噩梦。想象一下在启动时遇到 spring 错误 - 解决它的唯一方法是打开所有 jar 以找到其中包含的任何应用程序上下文。

    至少将应用程序上下文文件放在一个不同的包中,并指定您希望使用的任何名称。

    类路径根目录下的 *.xml 是灾难的根源。

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 2011-05-05
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 2011-04-01
      相关资源
      最近更新 更多