spring如何实现两个xml配置文件间的互调

这篇文章主要介绍了spring如何实现两个xml配置文件间的互调,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

首先建两个测试类

package soundsystem;
public class Dog {
  private String Cry;
  private Cat Cat;
  public void setCry(String cry) {
    Cry = cry;
  }
  public void setCat(soundsystem.Cat cat) {
    Cat = cat;
  }
  public void DogCry(){
    System.out.println("狗叫:"+Cry);
    Cat.CatCry();
  }
}
package soundsystem;
public class Cat {
  private String Cry;
  public Cat(String cry){
    this.Cry=cry;
  }
  public void CatCry(){
    System.out.println("猫叫:"+Cry);
  }
}

然后针对两类建两个xml配置文件

Bean_DogXML.xml

<?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">

  <import resource="Bean_CatXML.xml"></import>

  <bean id="Dog" class="soundsystem.Dog">
    <property name="Cry" value="汪汪汪~"></property>
    <property name="cat" ref="Cat"></property>
  </bean>

</beans>

Bean_CatXML.xml

<?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="Cat" class="soundsystem.Cat">
    <constructor-arg value="喵喵~"></constructor-arg>
  </bean>
</beans>

现在开始测试:

package Test;

import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import soundsystem.Cat;
import soundsystem.Dog;
@RunWith(SpringJUnit4ClassRunner.class)
public class Test {
  @org.junit.Test
  public static void main(String[] args) {
    ApplicationContext ap=new ClassPathXmlApplicationContext("config/Bean_DogXML.xml");
    Dog dog=(Dog)ap.getBean("Dog");
    dog.DogCry();
  }
}

输出结果是:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • spring mvc 读取xml文件数据库配置参数的方法

    本文主要介绍怎么通过属性注入与构造器注入实现把我们项目中要用到的数据库参数放到xml文件里面去,方便部署. spring mvc 4.2.6项目 SQL Server 2008数据库 本文介绍的主要使用ApplicationContext以及其实现类实现.主要用到的是ClassPathXmlApplicationContext. ClassPathXmlApplicationContext:从类路径ClassPath中寻找指定的XML配置文件,找到并装载 完成ApplicationContext

  • SpringBoot通过yml和xml文件配置日志输出方法

    SpringBoot中默认使用Logback进行日志输出,可以同时使用SpringBoot框架的配置文件application.yml或是通过logback的配置文件logback.xml进行配置. 通过application.yml配置 <?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--定义日志文件的存储地址 勿在 Lo

  • Spring根据XML配置文件 p名称空间注入属性的实例

    要生成对象并通过名称空间注入属性的类 代码如下: package com.swift; public class User { private String userName; public void setUserName(String userName) { this.userName = userName; } public String fun() { return "User's fun is ready."+this.userName; } } XML配置文件写法如下: &

  • Spring根据XML配置文件注入属性的方法

    方法一使用setter方法 package com.swift; public class Book { private String bookName; public void setBook(String bookName) { this.bookName = bookName; } @Override public String toString() { return "Book [book=" + bookName + "]"; } } 在Spring框架中

  • Spring主配置文件(applicationContext.xml) 导入约束详解

    eclipse导入Spring配置文件约束  Windows-Preference-XML-XMLCatalog 点 Add 选File System 下spring的解压包下的schema文件夹,选beans,然后选择spring对应的版本的xsd文件 选择指定xsd文件,再Key的路径后面添加"/spring-beans-4.2.xsd"点ok 创建applicationContext.xml   写根元素 <beans></beans> Add导入XSI,

  • Spring 配置文件XML头部文件模板实例详解

    普通spring配置文件模板: <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.s

  • Spring如何在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 <beans> 根元素,可包含多个<bean>元素,一个<bean>即一个Bean的配置. <bean> 一个<bean>即一个Bean对象.原来是new出来该类的一个对象,Spring中是一个<bean>创建一个对象. <bean na

  • 详解spring applicationContext.xml 配置文件

    applicationContext.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http

  • spring如何实现两个xml配置文件间的互调

    这篇文章主要介绍了spring如何实现两个xml配置文件间的互调,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 首先建两个测试类 package soundsystem; public class Dog { private String Cry; private Cat Cat; public void setCry(String cry) { Cry = cry; } public void setCat(soundsystem.Cat c

  • Spring 加载多个xml配置文件的原理分析

    目录 示例 spring-configlication.xml: spring-config-instance-factory.xml java示例代码 实现 AbstractRefreshableConfigApplicationContext AbstractApplicationContext AbstractRefreshableApplicationContext AbstractXmlApplicationContext 示例 先给出两个Bean的配置文件: spring-confi

  • Spring boot AOP通过XML配置文件声明

    通过 XML 配置文件声明 在前两篇博文和示例中,我们已经展示了如何通过注解配置去声明切面,下面我们看看如何在 XML 文件中声明切面.下面先列出 XML 中声明 AOP 的常用元素: AOP配置元素 用途 aop:advisor 定义AOP通知器 aop:after 定义AOP后置通知(不管被通知的方法是否执行成功) aop:after-returning 定义AOP返回通知 aop:after-throwing 定义AOP异常通知 aop:around 定义AOP环绕通知 aop:aspec

  • Spring boot AOP通过XML配置文件声明的方法

    通过 XML 配置文件声明 在前两篇博文和示例中,我们已经展示了如何通过注解配置去声明切面,下面我们看看如何在 XML 文件中声明切面.下面先列出 XML 中声明 AOP 的常用元素: AOP配置元素 用途 aop:advisor 定义AOP通知器 aop:after 定义AOP后置通知(不管被通知的方法是否执行成功) aop:after-returning 定义AOP返回通知 aop:after-throwing 定义AOP异常通知 aop:around 定义AOP环绕通知 aop:aspec

  • spring是如何解析xml配置文件中的占位符

    前言 我们在配置Spring Xml配置文件的时候,可以在文件路径字符串中加入 ${} 占位符,Spring会自动帮我们解析占位符,这么神奇的操作Spring是怎么帮我们完成的呢?这篇文章我们就来一步步揭秘. 1.示例 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setConfigLocation("${java.versi

  • spring*.xml配置文件明文加密的实现

    说明:客户要求spring*.xml中Oracle/Redis/MongoDB的IP.端口.用户名.密码不能明文存放,接到需求的我,很无奈,但是还是的硬着头皮搞 系统架构:spring+mvc(Oracle是用jdbc自己封装的接口) 1.数据库配置文件加密 原xml配置 <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/

  • Spring中xml配置文件的基础使用方式详解

    目录 1. xml配置文件的读取 1.1 通过类路径读取配置文件 1.2 通过文件系统绝对路径读取配置文件 1.3使用BeanFactory接口读取配置文件 2.带参构造对象的创建(constructor-arg标签) 3.使用另一个类中的方法创建对象,并放到Spring容器中 4.调用另一个类中的静态方法创建对象,并放到Spring容器中 5.对象的生命周期 6.单例多例的测试 1. xml配置文件的读取 目录结构 applicationContext.xml配置文件 <?xml versio

  • Spring手动生成web.xml配置文件过程详解

    步骤一: 找到自己所创建的项目名,效果如下: 步骤二: 右击自己所创建的项目---->Java EE Tools---->点击Generate Deployment Descriptor Stub,完成这几步,即可,效果如下: 最后,就会生成web.xml配置文件会在WebContent-->WEB-INF文件中,如下: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们.

随机推荐