您好,登錄后才能下訂單哦!
這篇文章給大家介紹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
<?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
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"。
<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
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="絡南街道"></bean> <!-- 要求配置Student時,要依賴于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允許用戶通過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的關系是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。