您好,登錄后才能下訂單哦!
這篇文章給大家介紹spring與bean的關(guān)系是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
一、繼承關(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
<?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="絡(luò)南街道"></bean> <!-- 使用parent指定指定哪個(gè)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.創(chuàng)建spring的IOC容器對(duì)象 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來(lái)表明一個(gè)Bean是一個(gè)抽象bean。抽象bean可以作為一個(gè)模板,且不能被實(shí)例化。同時(shí),如果一個(gè)bean沒(méi)有聲明class,那么該bean也是一個(gè)抽象bean,且必須指定abstract="true"。
<bean id="address" class="com.gong.spring.beans.autowire.Address" abstract="true" p:city="武漢" p:street="絡(luò)南街道"></bean>
此時(shí),在進(jìn)行實(shí)例化就會(huì)報(bào)錯(cuò)
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
<?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="絡(luò)南街道"></bean> <!-- 要求配置Student時(shí),要依賴于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允許用戶通過(guò)depends-on屬性設(shè)定bean前置依賴bean,前置依賴bean會(huì)在本Bean實(shí)例化之前就創(chuàng)建好。如果前置依賴于多個(gè)Bean,則可以通過(guò)逗號(hào),空格的方式來(lái)配置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容器對(duì)象 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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。