這篇文章給大家介紹spring與bean的關(guān)系是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、什邡網(wǎng)絡(luò)推廣、成都微信小程序、什邡網(wǎng)絡(luò)營銷、什邡企業(yè)策劃、什邡品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供什邡建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
一、繼承關(guān)系
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.創(chuàng)建spring的IOC容器對象 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); //2.從容器中獲取Bean實(shí)例 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=武漢。
當(dāng)然,我們也可以使用abstract來表明一個Bean是一個抽象bean。抽象bean可以作為一個模板,且不能被實(shí)例化。同時,如果一個bean沒有聲明class,那么該bean也是一個抽象bean,且必須指定abstract="true"。
此時,在進(jìn)行實(shí)例化就會報(bào)錯
Address address = (Address) ctx.getBean("address");
將抽象bean作為父bean,可以實(shí)例化它的子bean:
Address address2 = (Address) ctx.getBean("address2"); System.out.println(address2.toString());
二、依賴關(guān)系
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屬性設(shè)定bean前置依賴bean,前置依賴bean會在本Bean實(shí)例化之前就創(chuàng)建好。如果前置依賴于多個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.創(chuàng)建spring的IOC容器對象 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); //2.從容器中獲取Bean實(shí)例 Student student = (Student) ctx.getBean("student"); System.out.println(student.toString()); } }
輸出:
關(guān)于spring與bean的關(guān)系是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。