java - 如何在 Maven 依赖项中使用 Spring 配置文件

标签 java spring dependencies

在依赖项 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

我确信有一种方法可以做到这一点,但是我还没有找到。有谁知道怎么做吗?

最佳答案

尝试使用 classpath*: 前缀。

关于java - 如何在 Maven 依赖项中使用 Spring 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2647099/

相关文章:

java - 以静默模式安装 java 1.7 runtime jre-7u4(无用户交互)

java - 我是否需要在 DAO 实现中使用 setter 或构造函数

java - 将参数注入(inject) spring-data 动态查询构建方法

java - 创建 "Service"bean 时出错

java - Maven2 : Renaming provided dependecies at package phase?

java - 10.3 中包含 javax.servlet 的 WebLogic Jar 在哪里?

java - 如何在 selenium webdriver 中将网络浏览器从 Firefox 更改为 Chrome/Opera/IE/Safari?

spring - EJB3 拦截器无法引导上下文

visual-c++ - Visual Studio : Automatic COM registration with dependant DLLs outside Debug/Release dirs

java - 用grails +工作流+ Java开发一个Web应用程序,如何将它们集成在一起?