Java 引用另一个.xml文件中定义的bean

Java 引用另一个.xml文件中定义的bean,java,spring,Java,Spring,我有两个spring应用程序上下文XML: A.xml B.xml ... 文件A.xml不在我的控制之下,因此我无法使用导入B.xml,但B.xml导入A.xml,因此它完全是另一种方式 由于允许的SpEL语法{getObject('bBean')},aBean始终被实例化为空 有没有办法克服这个问题 谢谢 这是有效的: 主类: package com.example; import com.example.route.A; import org.springframework.bo

我有两个spring应用程序上下文XML:

A.xml


B.xml


...
文件A.xml不在我的控制之下,因此我无法使用
导入B.xml,但B.xml导入A.xml,因此它完全是另一种方式

由于允许的
SpEL
语法
{getObject('bBean')}
,aBean始终被实例化为空

有没有办法克服这个问题

谢谢

这是有效的:

主类

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>
B类

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>
/resources/ctxt/A.xml

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>

/resources/ctxt/B.xml

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>

这项工作:

主类

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>
B类

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>
/resources/ctxt/A.xml

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>

/resources/ctxt/B.xml

package com.example;

import com.example.route.A;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(locations = {"classpath*:/ctxt/B.xml"})
public class DemoApplication {


    public static void main(String[] args) {
        ApplicationContext ctxt = SpringApplication.run(DemoApplication.class, args);

        A a = ctxt.getBean(A.class);
        System.out.println(a.toString());   // A{b=com.example.route.B@5488b5c5}   <-- A init correctly with non-null B


    }
}
package com.example.route;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("A{");
        sb.append("b=").append(b);
        sb.append('}');
        return sb.toString();
    }
}
package com.example.route;

public class B {
}
<?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="aBean" class="com.example.route.A">
        <constructor-arg name="bBean" value="#getObject('bBean')"/>
    </bean>
</beans>
<?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="bBean" class="com.example.route.B"></bean>

    <import resource="classpath*:ctxt/A.xml"></import>
</beans>


此项目工作正常。然而,我最终得到了另一个文件A.xml,我在其中定义了bBean,因此在类路径中找到的两个名为A.xml的文件合并到一个文件中。在我的项目中不起作用,可能是因为加载整个依赖关系树的xml的方式很奇怪,其中包括约100个模块。如果它在您的大项目中不起作用,请检查您的类路径。如果您正在创建一个大jar,打开它,看看xml文件在哪里,很可能您在“classpath*:”字符串文字定义中没有正确引用它。classpath应该可以,因为a.xml位于a.jar的根级别,包含在B中作为第二级依赖项,如:B->(其他模块)->A.使用intellij idea spring插件并单击
可正确解析该文件。此项目工作正常。然而,我最终得到了另一个文件A.xml,我在其中定义了bBean,因此在类路径中找到的两个名为A.xml的文件合并到一个文件中。在我的项目中不起作用,可能是因为加载整个依赖关系树的xml的方式很奇怪,其中包括约100个模块。如果它在您的大项目中不起作用,请检查您的类路径。如果您正在创建一个大jar,打开它,看看xml文件在哪里,很可能您在“classpath*:”字符串文字定义中没有正确引用它。classpath应该可以,因为a.xml位于a.jar的根级别,包含在B中作为第二级依赖项,如:B->(其他模块)->A.使用intellij idea spring插件并单击
可正确解析该文件。