Spring中Bean的作用域和自動裝配方式
Bean的作用域
Spring中bean的作用域共有singleton、prototype、request、session、application、websocket六種

其中后四種都是用在Web應(yīng)用程序中的,主要介紹前兩種singleton(單例)和prototype(原型)
Bean的作用域范圍為singleton時,所有實例共享一個對象。
Spring的默認(rèn)配置為scope = “singleton”,以下兩種配置的效果是一樣的:
默認(rèn)配置
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Spring默認(rèn)配置為scope = "singleton"-->
<bean id = "user" class="indi.stitch.pojo.User" />
</beans>
scope = “singleton”
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Spring默認(rèn)配置為scope = "singleton"-->
<bean id = "user" class="indi.stitch.pojo.User" scope = "singleton" />
</beans>
測試類及輸出結(jié)果:
import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
User user = context.getBean("user", User.class);
User user2 = context.getBean("user", User.class);
System.out.println(user == user2);
}
}

scope = “prototype”
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Spring默認(rèn)配置為scope = "singleton"-->
<bean id = "user" class="indi.stitch.pojo.User" scope = "prototype" />
</beans>
測試類及輸出結(jié)果:
import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
User user = context.getBean("user", User.class);
User user2 = context.getBean("user", User.class);
System.out.println(user == user2);
}
}

Bean的自動裝配
Spring中Bean的自動裝配基于autowired標(biāo)簽實現(xiàn)
首先創(chuàng)建實體類People、Cat、Dog,People和Cat、Dog是組合關(guān)系,People中定義了依賴于Cat、Dog的屬性
People實體類
package indi.stitch.pojo;
public class People {
private Cat cat;
private Dog dog;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
'}';
}
}
Cat實體類
package indi.stitch.pojo;
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
Dog實體類
package indi.stitch.pojo;
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
通過name自動裝配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "cat" class="indi.stitch.pojo.Cat" />
<bean id = "dog" class="indi.stitch.pojo.Dog" />
<!--在Spring上下文中通過檢索name完成自動裝配,檢索依據(jù)為bean中屬性的set方法除set部分外的后綴-->
<bean id = "people" class="indi.stitch.pojo.People" autowire="byName"/>
</beans>
測試類及輸出結(jié)果:
import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
輸出結(jié)果

通過type自動裝配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "cat" class="indi.stitch.pojo.Cat" />
<bean id = "dog" class="indi.stitch.pojo.Dog" />
<!--在Spring上下文中通過對屬性對應(yīng)類型進行檢索完成自動裝配,Spring配置中不能存在被依賴的相同類型的多個bean,被依賴的bean在Spring中配置時可以省略id屬性-->
<bean id = "people" class="indi.stitch.pojo.People" autowire="byType"/>
</beans>
測試類和結(jié)果和上面相同
import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
輸出結(jié)果

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java反射技術(shù)實現(xiàn)簡單IOC容器
這篇文章主要介紹了基于Java反射技術(shù)實現(xiàn)簡單IOC容器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
SpringBoot熱部署Springloaded實現(xiàn)過程解析
這篇文章主要介紹了SpringBoot熱部署Springloaded實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
SpringMVC4 + MyBatis3 + SQL Server 2014整合教程(含增刪改查分頁)
這篇文章主要給大家介紹了關(guān)于SpringMVC4 + MyBatis3 + SQL Server 2014整合的相關(guān)資料,文中包括介紹了增刪改查分頁等相關(guān)內(nèi)容,通過示例代碼介紹的非常詳細(xì),分享出來供大家參考學(xué)習(xí),下面來一起看看吧。2017-06-06
java 中Object與Objects的區(qū)別在哪里
這篇文章主要介紹了java 中Object與Objects的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
使用Java實現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫
這篇文章主要介紹了使用Java實現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07

