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

其中后四種都是用在Web應(yīng)用程序中的,主要介紹前兩種singleton(單例)和prototype(原型)
Bean的作用域范圍為singleton時(shí),所有實(shí)例共享一個(gè)對象。
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的自動(dòng)裝配
Spring中Bean的自動(dòng)裝配基于autowired標(biāo)簽實(shí)現(xiàn)
首先創(chuàng)建實(shí)體類People、Cat、Dog,People和Cat、Dog是組合關(guān)系,People中定義了依賴于Cat、Dog的屬性
People實(shí)體類
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實(shí)體類
package indi.stitch.pojo;
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
Dog實(shí)體類
package indi.stitch.pojo;
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
通過name自動(dòng)裝配
<?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完成自動(dòng)裝配,檢索依據(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自動(dòng)裝配
<?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)類型進(jìn)行檢索完成自動(dòng)裝配,Spring配置中不能存在被依賴的相同類型的多個(gè)bean,被依賴的bean在Spring中配置時(shí)可以省略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é)果

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java反射技術(shù)實(shí)現(xiàn)簡單IOC容器
這篇文章主要介紹了基于Java反射技術(shù)實(shí)現(xiàn)簡單IOC容器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java使用阻塞隊(duì)列控制線程通信的方法實(shí)例詳解
這篇文章主要介紹了Java使用阻塞隊(duì)列控制線程通信的方法,結(jié)合實(shí)例形式詳細(xì)分析了java使用阻塞隊(duì)列控制線程通信的相關(guān)原理、方法及操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
SpringBoot熱部署Springloaded實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringBoot熱部署Springloaded實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
java 實(shí)現(xiàn)切割文件和合并文件的功能
這篇文章主要介紹了java 實(shí)現(xiàn)切割文件和合并文件的功能的相關(guān)資料,這里實(shí)現(xiàn)文件的切割的實(shí)現(xiàn)代碼和文件合并的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-07-07
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ū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
cmd中javac和java使用及注意事項(xiàng)詳解
這篇文章主要介紹了cmd中javac和java使用及注意事項(xiàng)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫
這篇文章主要介紹了使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07

