spring之bean之间的关系

spring之bean之间的关系,第1张

概述两种关系:继承、依赖 一、继承关系 Address.java package com.gong.spring.beans.autowire; public class Address { privat

两种关系:继承、依赖

一、继承关系

Address.java

package com.gong.spring.beans.autowire;public class Address {    private String city;     String street;        public String getCity() {        return city;    }    voID setCity(String city) {        this.city = String getStreet() {         street;    }     setStreet(String street) {        this.street = street;    }    @OverrIDe     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

import org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; Main {    static  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());    }}

输出:

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"); 

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

    Address address2 = (Address) ctx.getBean("address2");     System.out.println(address2.toString());

二、依赖关系

Car.java

 Car {         Car() {    }     Car(String name) {        this.name = name;    }     String name;     String getname() {         name;    }     setname(String name) {         name;    }    @OverrIDe    return "Car [name=" + name + "]";    }    }

Student.java

 com.gong.spring.beans.autowire; java.util.List; java.util.Map; Student {         String name;    private int age;    double score;     Car car;     Address address;     getAge() {         age;    }    voID setAge( age) {        this.age = getscore() {         score;    }    voID setscore( score) {        this.score = Car getCar() {         car;    }     setCar(Car car) {        this.car = Address getAddress() {         address;    }     setAddress(Address address) {        this.address = address;    }    @OverrIDe    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>    ="address"="com.gong.spring.beans.autowire.Address"     p:city="武汉" p:street="络南街道"<!-- 要求配置Student时,要依赖于Car-->    ="student"="com.gong.spring.beans.autowire.Student"="tom"    p:age="12" p:score="99.00" autowire="byname" depends-on="car"></beans>

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

Main.java

2.从容器中获取Bean实例        Student student = (Student) ctx.getBean("student");         System.out.println(student.toString());    }}

输出:

总结

以上是内存溢出为你收集整理的spring之bean之间的关系全部内容,希望文章能够帮你解决spring之bean之间的关系所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1226966.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存