最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中Bean的作用域和自動裝配方式

 更新時間:2021年09月28日 09:21:37   作者:想飛的魚Stitch  
這篇文章主要介紹了Spring中Bean的作用域和自動裝配方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Bean的作用域

Spring中bean的作用域共有singleton、prototype、request、session、application、websocket六種

其中后四種都是用在Web應(yīng)用程序中的,主要介紹前兩種singleton(單例)和prototype(原型)

Bean的作用域范圍為singleton時,所有實例共享一個對象。

Spring的默認配置為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默認配置為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默認配置為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默認配置為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)文章

最新評論

博白县| 华亭县| 柳河县| 南阳市| 郎溪县| 昆明市| 富宁县| 吴堡县| 大田县| 沁阳市| 德格县| 平昌县| 和龙市| 天长市| 纳雍县| 遂溪县| 新建县| 峨边| 九台市| 汉沽区| 张掖市| 台东市| 宾川县| 北海市| 固镇县| 肃宁县| 泸州市| 句容市| 林芝县| 太和县| 庄浪县| 黑龙江省| 原平市| 南岸区| 鲜城| 灵宝市| 景洪市| 深州市| 克山县| 谷城县| 岐山县|