当前位置: 首页 > 知识库问答 >
问题:

如何在spring中连接父XML和子XML

徐新荣
2023-03-14

我想要的是,在不编写setup()方法的情况下,如何连接(或引用)两个xml(parent-xml和child-xml)?

在setup()方法中,有一些使context.xml child-xml connect(连接context.xml和child-xml)的逻辑(cording)。但我认为还有另一种方法可以把它们联系起来。Alredy我试着写了

import resource="classpath:/test/src/main/resources/context.xml" 

在contextsub.xml中,但它不起作用。你能告诉我一声吗?有什么错误的绳子吗?

<?xml version="1.0" encoding="UTF-8"?>
<bean id="hello" class="com.nana.service.Hello">
    <property name="name" >
        <value>HongSangJik</value>
    </property>
    <property name="printer" ref="consolePrint" />
</bean>

<bean id="consolePrint" class="com.nana.service.ConsolePrinter" />

******contextsub.xml******

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans.xsd“>

<bean id="helloSub" class="com.nana.service.Hello">
    <property name="name">
        <value>HongGilDong</value>
    </property>
    <property name="printer" ref="consolePrint" />
</bean>

********HelloTest.java********

 package com.nana.service;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="/context.xml")

    public class HelloTest {
    ApplicationContext context;
    ApplicationContext subContext;
    Hello hello;
    Hello hellosub;

    @Before
    public void setup() {
        context= 
                new GenericXmlApplicationContext("/context.xml");
        hello = context.getBean("hello", Hello.class);
        GenericApplicationContext child= 
                new GenericApplicationContext(context);
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(child);
        reader.loadBeanDefinitions("contextSub.xml");
        child.refresh();
        hellosub=child.getBean("helloSub", Hello.class);
    }

    @Test
    public void sayHelloTest() {
        assertThat("Hello!HongSangJik", is(hello.sayHello()));
        assertThat("Hello!HongGilDong", is(hellosub.sayHello()));

    }
}
package com.nana.service;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/context.xml")


public class HelloTest {
ApplicationContext context;
ApplicationContext subContext;
Hello hello;
Hello hellosub;

@Before
public void setup() {

    context= new GenericXmlApplicationContext("/context.xml");
    hello = context.getBean("hello", Hello.class);
    hellosub=context.getBean("helloSub", Hello.class);

}

@Test
public void sayHelloTest() {
    assertThat("Hello!HongSangJik", is(hello.sayHello()));
    assertThat("Hello!HongGilDong", is(hellosub.sayHello()));

}
<?xml version="1.0" encoding="UTF-8"?>
<bean id="helloSub" class="com.nana.service.Hello">
    <property name="name">
        <value>HongGilDong</value>
    </property>
    <property name="printer" ref="consolePrint" />
</bean>

共有1个答案

扶珂
2023-03-14

由于您的context.xml是parent-xml,而contextsub.xml是child-xml,所以我将尝试在context.xml中添加这一行:

<import resource="contextSub.xml"/>

并从contextsub.xml文件中删除

在java代码中,我们只需要加载“context.xml”文件。

@Before
public void setup()
{
    context= new GenericXmlApplicationContext("/context.xml");
    hello = context.getBean("hello", Hello.class);
    hellosub=context.getBean("helloSub", Hello.class);
}
 类似资料:
  • 如何通过Xml配置连接JpaRepository的a(子类/子接口)? 所以我有一个JpaRepository的“实现” 和实体 和一个我需要注入MyDepartmentJpaRepo的类 以及“管理器”的界面,以确保完整性。 问题出在应用程序上下文.xml。 所以……spring-boot-data让人们将(子接口JpaRepository)定义为一个INTERFACE 因此,当您尝试用xml定

  • 我有一个很好用的Spring控制器: null

  • 如何将类author中的author\u firstName和author\u lastName与类目绑定?我使用h2数据库,作者id和图书id是主键,我使用postman

  • 问题内容: 假设我有下表: 并且我想创建下表: 其中branch_id表示通过parent_id链接在一起的分组。 但是, 不能保证行顺序 , 分支可能包含数百行 。这排除了LAG()函数的简单使用。 鉴于BigQuery的SQL的局限性,我该如何实现? 问题答案: 下面的示例说明了如何在这种情况下使用BigQuery脚本 最终输出

  • 我想得到由接口实现的子类名。例如 在我的代码中,我声明了接口A,并将值设置为其中一个类。所以我有这样的: 现在我想得到当前的子类名称,比如B、C或其他什么,但我不想用instanceof,因为我有很多子类,这使得代码不必要太长。我基本上是在寻找这个,但是getChild()当然不存在。 提前谢谢!

  • 我正在使用spring-data-mongoDB 1.10.12和mongo 3.6.4。我最近从一个较低版本的mongo进行了升级,现在我的mongo连接池监控中断了,因为没有注册ConnectionPoolStatisticsMBean。根据mongo版本的文档,“默认情况下禁用JMX连接池监视。要启用它,请通过MongoClientOptions添加com.mongodb.managemen