大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章给大家介绍spring与bean的关系是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、网站制作、什邡网络推广、成都微信小程序、什邡网络营销、什邡企业策划、什邡品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供什邡建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
一、继承关系
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
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"。
此时,在进行实例化就会报错
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
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的关系是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。