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

网友投稿 644 2023-07-01

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

通过实例解析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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

p:city="武汉" p:street="络南街道">

p:street="珞狮街道">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

p:city="武汉" p:street="络南街道">

p:city="武汉" p:street="络南街道">

p:street="珞狮街道">

p:street="珞狮街道">

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

}

}

输出:

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

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

p:city="武汉" p:street="络南街道">

p:city="武汉" p:street="络南街道">

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

Address address = (Address) ctx.getBean("address");

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

Address address2 = (Address) ctx.getBean("address2");

System.out.println(address2.toString());

二、依赖关系

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

p:city="武汉" p:street="络南街道">

p:age="12" p:score="99.00" autowire="byName" depends-on="car">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

p:city="武汉" p:street="络南街道">

p:city="武汉" p:street="络南街道">

p:age="12" p:score="99.00" autowire="byName" depends-on="car">

p:age="12" p:score="99.00" autowire="byName" depends-on="car">

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

}

}

输出:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring如何使用命名空间p简化bean的配置
下一篇:Spring AOP定义AfterReturning增加实例分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~