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

Spring中Bean的作用域和自動(dòng)裝配方式

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

Bean的作用域

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

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

Bean的作用域范圍為singleton時(shí),所有實(shí)例共享一個(gè)對(duì)象。

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>

測(cè)試類(lèi)及輸出結(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>

測(cè)試類(lèi)及輸出結(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í)體類(lèi)People、Cat、Dog,People和Cat、Dog是組合關(guān)系,People中定義了依賴(lài)于Cat、Dog的屬性

People實(shí)體類(lèi)

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í)體類(lèi)

package indi.stitch.pojo;
public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}

Dog實(shí)體類(lèi)

package indi.stitch.pojo;
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}

通過(guò)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上下文中通過(guò)檢索name完成自動(dòng)裝配,檢索依據(jù)為bean中屬性的set方法除set部分外的后綴-->
    <bean id = "people" class="indi.stitch.pojo.People" autowire="byName"/>
</beans>

測(cè)試類(lèi)及輸出結(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é)果

在這里插入圖片描述

通過(guò)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上下文中通過(guò)對(duì)屬性對(duì)應(yīng)類(lèi)型進(jìn)行檢索完成自動(dòng)裝配,Spring配置中不能存在被依賴(lài)的相同類(lèi)型的多個(gè)bean,被依賴(lài)的bean在Spring中配置時(shí)可以省略id屬性-->
    <bean id = "people" class="indi.stitch.pojo.People" autowire="byType"/>
</beans>

測(cè)試類(lèi)和結(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)文章

最新評(píng)論

精河县| 桓仁| 苏尼特右旗| 枝江市| 四会市| 准格尔旗| 天长市| 阿荣旗| 方山县| 琼中| 游戏| 哈巴河县| 翼城县| 五家渠市| 黄石市| 邹城市| 上林县| 保康县| 泰州市| 青海省| 天全县| 海淀区| 昌都县| 寿光市| 太白县| 靖宇县| 旺苍县| 桐梓县| 冀州市| 象山县| 卢氏县| 佳木斯市| 武宣县| 大同县| 准格尔旗| 修水县| 东阿县| 福安市| 宁晋县| 泾源县| 恩施市|