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

Spring(二):Spring通過IOC來創(chuàng)建對象

 更新時間:2021年07月06日 17:27:20   作者:熬夜加班寫代碼  
下面小編就為大家?guī)硪黄斦凷pring對IOC的理解(推薦篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、IOC如何獲取對象

1.1 Spring是如何獲取對象的?

①新建一個maven項目后導(dǎo)入webmvc的依賴:因為webmvc包含了很多其他依賴,為了省事,干脆導(dǎo)入一個總的,方便省事!版本嘛!個人比較喜歡用最新版。

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.5</version>
    </dependency>

②新建實體測試類:

public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
    //get、set、tostring方法為了篇幅省略,可以自己加或者使用lombok
}

③在resources目錄下新建ContextAplication.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="Person" class="entity.Person">
        <property name="age" value="23"></property>
        <property name="name" value="丁大大"></property>
        <property name="like" value="釣魚"></property>
        <property name="high" value="173"></property>
    </bean>
</beans>

④以上前提之后,你會發(fā)現(xiàn)你的測試Person類種發(fā)生了變化:點擊可以跳轉(zhuǎn)到指定的xml位置哦~

在這里插入圖片描述

⑤測試:

Context.getBean() 不指定類時,需要強制轉(zhuǎn)換,所以建議使用第二種方式來獲取對象

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
//        Person person = (Person) Context.getBean("Person");//這里不指定的話需要強轉(zhuǎn),建議用下面的方式來拿對象
        Person person = Context.getBean("Person",Person.class);
        System.out.println(person);
    }
}

⑥執(zhí)行結(jié)果如下:成功拿到值!

在這里插入圖片描述

⑦總結(jié):

  • 控制: 傳統(tǒng)的程序?qū)ο蟮膭?chuàng)建是由程序來控制創(chuàng)建的。
  • 反轉(zhuǎn): 交給Spring容器來創(chuàng)建對象,而程序只負(fù)責(zé)被動的接收對象。這就是反轉(zhuǎn)。
  • 依賴注入: 就是通過set方法來注入的。

1.2 改造案例由xml選擇創(chuàng)建對象

①xml:

 <bean id="StudentMapperImpl" class="mapper.impl.StudentMapperImpl"/>
    <bean id="TeacherMapperImpl" class="mapper.impl.TeacherMapperImpl"/>
    <bean id="PersonServiceImpl" class="service.impl.PersonServiceImpl">
        <property name="studentMapper" ref="StudentMapperImpl"/>
    </bean>

②測試:

        ApplicationContext Context1 = new ClassPathXmlApplicationContext("ContextAplication.xml");
        PersonServiceImpl personServiceImpl = Context1.getBean("PersonServiceImpl", PersonServiceImpl.class);
        personServiceImpl.getPersonInfo();

③執(zhí)行結(jié)果:

在這里插入圖片描述

⑤總結(jié):

對象由Spring 來創(chuàng)建 , 管理 , 裝配 !這就是 IOC!

二、IOC是通過什么方式來創(chuàng)建對象的?

2.1 通過無參構(gòu)造函數(shù)來創(chuàng)建對象

①以Person類為例子,但是加上一個無參構(gòu)造函數(shù)!

public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
    public Person() {
        //輸出一句話證明自己被調(diào)用了!
        System.out.println("我是Person類的無參構(gòu)造函數(shù)!我被調(diào)用了?。。。?);
    }
    //set、get、tostring方法因為篇幅原因省略,請手動加上!
}

②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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="Person" class="entity.Person">
        <property name="age" value="23"></property>
        <property name="name" value="丁大大"></property>
        <property name="like" value="釣魚"></property>
        <property name="high" value="173"></property>
    </bean>
</beans>

③測試類:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("Person", Person.class);
        System.out.println(person);
    }
}

④執(zhí)行結(jié)果:

在這里插入圖片描述

⑤去除無參構(gòu)造,增加有參構(gòu)造:

在這里插入圖片描述

xml配置程序直接報錯:

在這里插入圖片描述

⑥總結(jié):

Spring創(chuàng)建對象默認(rèn)是通過無參構(gòu)造函數(shù)創(chuàng)建的!能通過有參構(gòu)造函數(shù)來創(chuàng)建對象嘛?能!看下面!

2.2 通過有參構(gòu)造方法來創(chuàng)建對象

①前提于 2.1 一致,新增有參構(gòu)造函數(shù):(因為類中,默認(rèn)的也就是不寫構(gòu)造參數(shù)就是無參構(gòu)造,寫了有參構(gòu)造才能真正意義上去除無參構(gòu)造,這個不用解釋太多吧,java基礎(chǔ)的內(nèi)容了~?。?/p>

    public Person(String name, int age, String like, String high) {
        this.name = name;
        this.age = age;
        this.like = like;
        this.high = high;
    }

②xml配置文件中要發(fā)生一定的改變:

<?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="Person" class="entity.Person">
<!--        <property name="name" value="丁大大"></property>-->
<!--        <property name="age" value="23"></property>-->
<!--        <property name="like" value="釣魚"></property>-->
<!--        <property name="high" value="173"></property>-->
        <constructor-arg index="0" value="丁大大"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="釣魚"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

③執(zhí)行結(jié)果:

在這里插入圖片描述

⑤總結(jié):

  • 無參構(gòu)造函數(shù)指定值時使用 propert 標(biāo)簽
  • 有參構(gòu)造函數(shù)指定值時使用 constructor-arg 標(biāo)簽,三種寫法
    • index --通過下標(biāo)來給屬性賦值
    • name --通過屬性名稱來給屬性賦值
    • type -- 指定屬性的類型來給屬性賦值
      • 基本類型可以直接寫
      • 引用類型得加上全稱,如:java.lang.String
      • 位置跟index差不多,依次從上到下對應(yīng)屬性的從上到下。
  • 在配置文件加載的時候。其中管理的對象都已經(jīng)初始化了!

三、Spring的配置

3.1 alias(別名):

  • 為bean設(shè)置別名,可設(shè)置多個!

①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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <alias name="Person" alias="personAlias1"/>
    <alias name="Person" alias="personAlias2"/>
    <alias name="Person" alias="personAlias3"/>
    <bean id="Person" class="entity.Person">
        <constructor-arg index="0" value="丁大大"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="釣魚"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

②測試類:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("personAlias1", Person.class);
        System.out.println(person);
    }
}

③執(zhí)行結(jié)果:

在這里插入圖片描述

④總結(jié):講實話,這玩意用處不大,因為還有更好的方式來設(shè)置別名!

3.2 Bean的配置:

  • bean就相當(dāng)于java對象,由Spring創(chuàng)建和管理

①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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <alias name="Person" alias="personAlias1"/>
    <alias name="Person" alias="personAlias2"/>
    <alias name="Person" alias="personAlias3"/>
    <bean id="Person" name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="釣魚"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

②測試類:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("person4", Person.class);
        System.out.println(person);
    }
}

③執(zhí)行結(jié)果:

在這里插入圖片描述

④總結(jié):

  • id是bean的唯一標(biāo)識符
  • 如果沒有配置id,那么name相當(dāng)于標(biāo)識符,并且可以設(shè)置多個
  • name也是別名,可多個,并且可以通過 逗號 空格 分號 來分隔,是不是比alias別名方便?所以設(shè)置別名我們一般使用name
  • id和name同時存在,name只是別名,不是標(biāo)識符
  • class是類的全限定名 包名+類名

在這里插入圖片描述

3.3 import(團(tuán)隊合作之導(dǎo)入)

①在實際工作的開發(fā)過程中,一個項目可能由多個程序員來進(jìn)行開發(fā),所以為了解決共性問題,比如:同一文件提交時都進(jìn)行了修改可能引起沖突,所以我們使用import來解耦!

②新建多個xml配置文件:

在這里插入圖片描述

ContextAplication.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="dyj1.xml"/>
    <import resource="dyj3.xml"/>
    <import resource="dyj2.xml"/>
</beans>

dyj1.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大1"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="釣魚1"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

dyj2.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大2"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="釣魚2"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

dyj3.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大3"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="釣魚3"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

③執(zhí)行:

在這里插入圖片描述

④總結(jié):

  • 如果三個文件都是對同一個操作同一個類,或者說內(nèi)容一致,那么就以主xml中從上到下最后一個impot為準(zhǔn)。
  • 語法格式:
  • 優(yōu)點:
    • 每個人開發(fā)的都是獨立的,如果重復(fù)的內(nèi)容,Spring會幫我們自動合并!
    • 降低了程序的沖突性!
    • 大大提高了后期代碼的可維護(hù)性!

總結(jié)

本篇文章就到這里了,希望能幫助到你,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • Java基礎(chǔ)入門 Swing中間容器的使用

    Java基礎(chǔ)入門 Swing中間容器的使用

    這篇文章主要介紹了Java基礎(chǔ)入門 Swing中間容器的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 在springboot文件中如何創(chuàng)建mapper.xml文件

    在springboot文件中如何創(chuàng)建mapper.xml文件

    這篇文章主要介紹了在springboot文件中如何創(chuàng)建mapper.xml文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • SpringIOC?BeanDefinition的加載流程詳解

    SpringIOC?BeanDefinition的加載流程詳解

    這篇文章主要為大家介紹了SpringIOC?BeanDefinition的加載流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java日期時間類(Date、DateFormat、Calendar)解析

    Java日期時間類(Date、DateFormat、Calendar)解析

    這篇文章主要介紹了Java日期時間類(Date、DateFormat、Calendar)解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • SpringBoot讀取配置優(yōu)先級順序的方法詳解

    SpringBoot讀取配置優(yōu)先級順序的方法詳解

    Spring Boot作為一種輕量級的Java應(yīng)用程序框架,以其開箱即用、快速搭建新項目的特性贏得了廣大開發(fā)者的青睞,在Spring Boot生態(tài)系統(tǒng)中,配置屬性可以從各種來源獲取,本文將深入探討Spring Boot加載外部配置屬性的優(yōu)先級規(guī)則,需要的朋友可以參考下
    2024-05-05
  • java面試常見問題---ConcurrentHashMap

    java面試常見問題---ConcurrentHashMap

    ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧
    2021-06-06
  • 詳解Java的內(nèi)存模型

    詳解Java的內(nèi)存模型

    本文更準(zhǔn)確的說法應(yīng)該是JVM的內(nèi)存模型,但是這里又牽扯了一些其他的前置知識,主要是想從Java入手,從源頭上梳理一遍整個Java底層運行的機制,中間會額外補充一些和題目無關(guān)的前置基礎(chǔ),導(dǎo)致主講內(nèi)存模型的篇幅所占的比例就不是那么絕對。
    2021-06-06
  • java獲取一個文本文件的編碼(格式)信息

    java獲取一個文本文件的編碼(格式)信息

    這篇文章主要介紹了java獲取一個文本文件的編碼(格式)信息,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Java的最大棧深度與JVM核心知識介紹

    Java的最大棧深度與JVM核心知識介紹

    這篇文章主要有兩個部分,一部分介紹JAVA的最大棧深度,第二部分介紹了JVM核心知識,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • Java中的Semaphore計數(shù)信號量詳細(xì)解析

    Java中的Semaphore計數(shù)信號量詳細(xì)解析

    這篇文章主要介紹了Java中的Semaphore計數(shù)信號量詳細(xì)解析,Semaphore?是一個計數(shù)信號量,必須由獲取它的線程釋放,常用于限制可以訪問某些資源的線程數(shù)量,例如通過?Semaphore?限流,需要的朋友可以參考下
    2023-11-11

最新評論

枞阳县| 青田县| 库伦旗| 北京市| 绥阳县| 菏泽市| 博野县| 常州市| 井研县| 且末县| 杭锦后旗| 喜德县| 鲁山县| 西乡县| 平定县| 朔州市| 溧水县| 丹棱县| 东方市| 灵川县| 电白县| 阳信县| 方正县| 高阳县| 碌曲县| 楚雄市| 徐闻县| 平阳县| 兴和县| 平南县| 五河县| 大港区| 波密县| 延安市| 全南县| 五大连池市| 图们市| 维西| 通江县| 利津县| 乌鲁木齐市|