欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

通过实例解析spring bean之间的关系

程序员文章站 2022-06-29 16:49:59
这篇文章主要介绍了通过实例解析spring bean之间的关系,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 两种关系:继承、依赖...

这篇文章主要介绍了通过实例解析spring bean之间的关系,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

两种关系:继承、依赖

一、继承关系

address.java

package com.gong.spring.beans.autowire;

public class address {
  private string city;
  private string street;
  
  public string getcity() {
    return city;
  }

  public void setcity(string city) {
    this.city = city;
  }

  public string getstreet() {
    return street;
  }

  public void setstreet(string street) {
    this.street = street;
  }

  @override
  public string tostring() {
    return "address [city=" + city + ", street=" + street + "]";
  }
}

beans-relation.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"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="address" class="com.gong.spring.beans.autowire.address" 
  p:city="武汉" p:street="络南街道"></bean>
  <!-- 使用parent指定指定哪个bean的配置,子bean可以覆盖父bean的配置 -->
  <bean id="address2" class="com.gong.spring.beans.autowire.address" parent="address" 
  p:street="珞狮街道"></bean>
</beans>

main.java

package com.gong.spring.beans.autowire;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class main {
  public static void main(string[] args) {
    //1.创建spring的ioc容器对象
    applicationcontext ctx = new classpathxmlapplicationcontext("beans-relation.xml");
    //2.从容器中获取bean实例
    address address = (address) ctx.getbean("address"); 
    system.out.println(address.tostring());
    address address2 = (address) ctx.getbean("address2"); 
    system.out.println(address2.tostring());
  }
}

输出:

通过实例解析spring bean之间的关系

address2继承了address的city配置,因此city=武汉。

当然,我们也可以使用abstract来表明一个bean是一个抽象bean。抽象bean可以作为一个模板,且不能被实例化。同时,如果一个bean没有声明class,那么该bean也是一个抽象bean,且必须指定abstract="true"。

<bean id="address" class="com.gong.spring.beans.autowire.address" abstract="true"
  p:city="武汉" p:street="络南街道"></bean>

此时,在进行实例化就会报错

address address = (address) ctx.getbean("address"); 

通过实例解析spring bean之间的关系

将抽象bean作为父bean,可以实例化它的子bean:

  address address2 = (address) ctx.getbean("address2"); 
  system.out.println(address2.tostring());

通过实例解析spring bean之间的关系

二、依赖关系

car.java

package com.gong.spring.beans.autowire;

public class car {
  
  public car() {
  }

  public car(string name) {
    this.name = name;
  }
  private string name;

  public string getname() {
    return name;
  }

  public void setname(string name) {
    this.name = name;
  }

  @override
  public string tostring() {
    return "car [name=" + name + "]";
  }
  
}

student.java

package com.gong.spring.beans.autowire;
import java.util.list;
import java.util.map;

public class student {
  
  private string name;
  private int age;
  private double score;
  private car car;
  private address address;
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public int getage() {
    return age;
  }
  public void setage(int age) {
    this.age = age;
  }
  public double getscore() {
    return score;
  }
  public void setscore(double score) {
    this.score = score;
  }
  public car getcar() {
    return car;
  }
  public void setcar(car car) {
    this.car = car;
  }
  public address getaddress() {
    return address;
  }
  public void setaddress(address address) {
    this.address = address;
  }
  @override
  public string tostring() {
    return "student [name=" + name + ", age=" + age + ", score=" + score + ", car=" + car + ", address=" + address
        + "]";
  }
  
  
}

beans-relation.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"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  
  <bean id="car" class="com.gong.spring.beans.autowire.car" p:name="baoma"></bean>
  <bean id="address" class="com.gong.spring.beans.autowire.address" 
  p:city="武汉" p:street="络南街道"></bean>
  <!-- 要求配置student时,要依赖于car-->
  <bean id="student" class="com.gong.spring.beans.autowire.student" p:name="tom"
  p:age="12" p:score="99.00" autowire="byname" depends-on="car"></bean>
</beans>

spring允许用户通过depends-on属性设定bean前置依赖bean,前置依赖bean会在本bean实例化之前就创建好。如果前置依赖于多个bean,则可以通过逗号,空格的方式来配置bean的名称。

main.java

package com.gong.spring.beans.autowire;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class main {
  public static void main(string[] args) {
    //1.创建spring的ioc容器对象
    applicationcontext ctx = new classpathxmlapplicationcontext("beans-relation.xml");
    //2.从容器中获取bean实例
    student student = (student) ctx.getbean("student"); 
    system.out.println(student.tostring());
  }
}

输出:

通过实例解析spring bean之间的关系

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