亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring:bean注入--Set方法注入

 更新時間:2021年07月16日 10:05:58   作者:寧在春  
這篇文章主要給大家總結(jié)介紹了關(guān)于Spring注入Bean的一些方式,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

Set 方法注入

1.新建一個空的 maven項目。

2.導(dǎo)入依賴

properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--這里是java 版本號-->
        <maven.compiler.source>11</maven.compiler.source>  
        <maven.compiler.target>11</maven.compiler.target>
        <!--這里是方便版本控制-->
        <spring.version>5.3.1</spring.version>
        <lombok.version>1.18.20</lombok.version>
        <junit.version>4.12</junit.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
    </dependency>
</dependencies>

3.工程項目結(jié)構(gòu)

在這里插入圖片描述

4.新建包 com.crush.pojo

5.新建Java類Student

@Data // set、get 方法
@AllArgsConstructor // 全參構(gòu)造
@NoArgsConstructor  // 無參構(gòu)造
public class Student {
    /**
     * 學(xué)號
     */
    private Long number;
    /**
     * 學(xué)生姓名
     */
    private String name;
    /**
     * 所在學(xué)校
     */
    private String school;
}

resource 下 beans.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">
    <!--第一種方式 set 方式注入
        1、有set方法才可以注入
        2、默認是單例模式 singleton
        -->
    <bean id="student" class="com.crush.pojo.Student" scope="singleton">
        <!--值可以跟在在標簽后面 也可以 寫在標簽內(nèi)部-->
        <property name="number">
            <value>1</value>
        </property>
        <property name="name" value="wyh"/>
        <property name="school" value="hngy"/>
    </bean>
        <!--這個id 就是 applicationContext.getBean("【bean-id】", Student.class);
        此處id 大多數(shù)時候命名規(guī)則就是 類名的第一個字母改為小寫
        class:Student
        bean id一般就為: student
     -->
    <bean id="ssss" class="com.crush.pojo.Student" scope="singleton">
        <!--值可以跟在在標簽后面 也可以 寫在標簽內(nèi)部-->
        <property name="number">
            <value>1</value>
        </property>
        <property name="name" value="wyh"/>
        <property name="school" value="hngy"/>
    </bean>
</beans>

寫一個測試類

public class Test {

    /**
     * 通過 ClassPathXmlApplicationContext 獲取 Spring 應(yīng)用程序的 上下文 ApplicationContext
     */
    @org.junit.Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        // 第一種方式 獲取ioc 容器中的Student 強制類型轉(zhuǎn)換
        Student student = (Student) applicationContext.getBean("student");
        // 第二種方式 直接在后面寫明類的標簽。
        Student student1 = applicationContext.getBean("student", Student.class);
        // student.setName("cccc"); 給其中一個修改 就會全部修改 可以自己打開測試下 
        System.out.println(student);
        System.out.println(student1);
        // 這里結(jié)果為true 
        // 解釋:因為Spring 默認構(gòu)造出來的對象 默認是單例的。 無論獲取多少次 ,都是單例的。
        System.out.println(student==student1);
    }
        /**
     * 通過 FileSystemXmlApplicationContext 獲取 Spring 應(yīng)用程序的 上下文 ApplicationContext
     * 還有第三種是 通過Web服務(wù)器實例化 ApplicationContext 容器
     */
    @org.junit.Test
    public void test2(){
        //這里的路徑 也可以 用絕對路徑
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src\\main\\resources\\beans.xml");
        Student student = applicationContext.getBean("student", Student.class);
        System.out.println(student);
    }
}

小小思考

為什么 new ClassPathXmlApplicationContext(“beans.xml”); 要用ApplicationContext 來接收,而不用ClassPathXmlApplicationContext 接收呢?

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

解釋:

按照面向接口編程的思想,聲明變量應(yīng)該是接口類型的,然后創(chuàng)建一個該接口的實現(xiàn)類的實例賦值給該變量。 ApplicationContext是接口,ClassPathXmlApplicationContext是它的一個實現(xiàn)類。所以你就看到了 ApplicationContext ac = new ClassPathXmlApplicationContext(…)

總結(jié)

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

相關(guān)文章

  • 小伙熬夜用Java重現(xiàn)經(jīng)典超級馬里奧代碼實例

    小伙熬夜用Java重現(xiàn)經(jīng)典超級馬里奧代碼實例

    這篇文章主要介紹了Java重現(xiàn)經(jīng)典超級馬里奧,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Spring?IOC容器Bean管理XML注入集合類型屬性

    Spring?IOC容器Bean管理XML注入集合類型屬性

    這篇文章主要為大家介紹了Spring?IOC容器Bean管理XML注入集合類型屬性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 在Java中Scanner的用法總結(jié)

    在Java中Scanner的用法總結(jié)

    這篇文章主要介紹了在Java中Scanner的用法總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中使用正則表達式獲取網(wǎng)頁中所有圖片的路徑

    Java中使用正則表達式獲取網(wǎng)頁中所有圖片的路徑

    這篇文章主要介紹了Java中使用正則表達式獲取網(wǎng)頁中所有圖片的路徑,本文直接給出實例代碼,需要的朋友可以參考下
    2015-06-06
  • Spring Boot Web應(yīng)用開發(fā) CORS 跨域請求支持

    Spring Boot Web應(yīng)用開發(fā) CORS 跨域請求支持

    本篇文章主要介紹了Spring Boot Web應(yīng)用開發(fā) CORS 跨域請求支持,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • springboot+vue實現(xiàn)阿里云oss大文件分片上傳的示例代碼

    springboot+vue實現(xiàn)阿里云oss大文件分片上傳的示例代碼

    阿里云推出了直傳,本文主要介紹了springboot+vue實現(xiàn)阿里云oss大文件分片上傳的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 手把手教你搞懂冒泡排序和選擇排序

    手把手教你搞懂冒泡排序和選擇排序

    這篇文章主要介紹了java數(shù)組算法例題代碼詳解(冒泡排序,選擇排序),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • java中的異或問題代碼解析

    java中的異或問題代碼解析

    這篇文章主要介紹了java中的異或問題代碼解析,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)

    Springboot搭建JVM監(jiān)控(Springboot + Prometheus +&n

    在應(yīng)用開發(fā)時,監(jiān)控報警必不可少,本文主要介紹了Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana),具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • SpringBoot集成Redis數(shù)據(jù)庫,實現(xiàn)緩存管理

    SpringBoot集成Redis數(shù)據(jù)庫,實現(xiàn)緩存管理

    SpringBoot2 版本,支持的組件越來越豐富,對Redis的支持不僅僅是擴展了API,更是替換掉底層Jedis的依賴,換成Lettuce。 本案例需要本地安裝一臺Redis數(shù)據(jù)庫。下面就來看下集成Redis的步驟
    2021-06-06

最新評論