Spring IOC 的簡單使用

Spring IOC (Inversion Of Control反轉控制容器java

1、對於IOC容器的簡單理解web

在java開發中將程序中的對象交給容器管理,而不是在對象的內部管理。spring

那麼兩個簡單的問題去分析理解IOC容器app

1.爲何叫反轉:框架

IOC容器實現了將對象的管理由對象內部直接管理(好比在一個類的main方法中new了一個String對象)轉換成在容器中被動的被注入,建立。把這種對對象管理方式的改變稱爲反轉。ide

2.控制了什麼:函數

IOC容器控制了外部資源的統一獲取(不單單包括對象,還有其餘的文件)測試

(敲下黑板:spring容器是IOC容器,可是IOC容器不是spring容器,由於基於IOC容器設計的框架不只只有spring)this

2、應用上下文spa

如今咱們手上有了一個魔杖(IOC容器)可是咱們還須要對應的魔咒(應用上下文)來驅動魔杖

spring容器有兩種實現:

1.最基礎的BeanFactory(實體工廠)因爲功能過於簡單因此不常常使用

2.由BeanFactory派生的多種應用上下文,其抽象接口是ApplicationContext,spring根據應用場景的不一樣給咱們設計了幾種應用上下文:

(1) AnnotationConfigApplicationContext:從一個或多個基於java的配置類中加載上下文定義,適用於java註解的方式;

(2)ClassPathXmlApplicationContext:從類路徑下的一個或多個xml配置文件中加載上下文定義,適用於xml配置的方式;

(3)FileSystemXmlApplicationContext:從文件系統下的一個或多個xml配置文件中加載上下文定義,也就是說系統盤符中加載xml配置文件;

(4)AnnotationConfigWebApplicationContext:專門爲web應用準備的,適用於註解方式;

咱們在這裏使用的是ClassPathXmlApplication,xml配置方式

3、簡單的實現IOC控制對象

1.建立一個應用上下文applicationContext_ioc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        ">
<beans>

2.建立一個類Student

package spring.ioc.unities;

public class Student {
    private String stuNo;
    private String stuName;
    private int gender;
    private String nativePlace;
    private String classNo;
    private String tel;
    public String getStuNo() {
        return stuNo;
    }
    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public int getGender() {
        return gender;
    }
    public void setGender(int gender) {
        this.gender = gender;
    }
    public String getNativePlace() {
        return nativePlace;
    }
    public void setNativePlace(String nativePlace) {
        this.nativePlace = nativePlace;
    }
    public String getClassNo() {
        return classNo;
    }
    public void setClassNo(String classNo) {
        this.classNo = classNo;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    @Override
    public String toString() {
        return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", gender=" + gender + ", nativePlace="
                + nativePlace + ", classNo=" + classNo + ", tel=" + tel + "]";
    }
    

}

3.在xml中經過bean標籤倆控制Bean

(1)標籤中必要的兩個屬性

id(用於識別ioc容器中的bean)

class(id對應的類的位置,一般是包名+類名,建議直接複製,手寫有可能出錯)


<bean id="stu1" class="spring.ioc.unities.Student"></bean>
 

(2)標籤中其餘的屬性

scope屬性:

 做用域  說明
 Singleton  

Spring IOC容器中一個bean定義只有一個對象實例,默認狀況下會在容器啓動時初始化

 

 Prototype 每次從容器獲取bean都是新的對象 
 Request  每次Http請求都會建立一個新的bean,只使用在WebApplicationContext中
 Session  相似request,每次有新的會話都會建立一個新的Bean,只使用在WebApplicationContext中

3.main函數中測試確認:

public static void main(String[] args) {
        

        ApplicationContext ioc =  new ClassPathXmlApplicationContext("applictionContext_ioc.xml"); 
        Student stuA = (Student) ioc.getBean("stu1");
        Student stuB = (Student) ioc.getBean("stu1");
        //在ioc中獲取的bean:
        System.out.println("獲取的屬性爲singleton的bean-------------------------");
        System.out.println(stuA.toString()+stuB.toString());
        //驗證A,B是不是同一個實體
        System.out.println("stua和stub是不是一個bean:"+(stuA==stuB));
        Student stuC = (Student) ioc.getBean("stu2");
        Student stuD = (Student) ioc.getBean("stu2");
        System.out.println("獲取的屬性爲prototype的bean-------------------------");
        System.out.println(stuC.toString()+stuD.toString());
        System.out.println("stuc和stud是不是一個bean:"+(stuC==stuD));
        
    }

得到結果以下:

能夠看出singleton屬性的bean實際上是單例模式下的bean.

相關文章
相關標籤/搜索