天天看點

Java練習(六):Spring應用之IoC (簡單的Student, Teacher工程)1. 背景概念2. 簡單的Student-Teacher工程,描述Spring中Ioc和DI的基本用法

目錄

1. 背景概念

IoC定義

2. 簡單的Student-Teacher工程,描述Spring中Ioc和DI的基本用法

2.1 maven系統的配置檔案:pom.xml

2.2 bean的配置檔案: bean_ioc.xml

2.3 類代碼:Student類,Teacher類

2.4 測試類(程式入口)

1. 背景概念

IoC定義

控制反轉:Spring通過一種稱作控制反轉IoC的技術促進了松耦合。

當應用了IoC, 一個對象依賴的其他對象會通過被動的方式傳遞進來,而不是這個對象自己建立或者查找依賴對象。

IoC容器的功能:執行個體化,初始化元件,裝配元件依賴關系,負責元件生命周期管理。

2. 簡單的Student-Teacher工程,描述Spring中Ioc和DI的基本用法

Java練習(六):Spring應用之IoC (簡單的Student, Teacher工程)1. 背景概念2. 簡單的Student-Teacher工程,描述Spring中Ioc和DI的基本用法

2.1 maven系統的配置檔案:pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.my.test</groupId>

  <artifactId>SpringIoCDemo</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <dependencies>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>5.3.3</version>

        </dependency>        

        <dependency>

            <groupId>org.aspectj</groupId>

            <artifactId>aspectjweaver</artifactId>

            <version>1.9.4</version>

        </dependency>

    </dependencies>  

</project>

2.2 bean的配置檔案: bean_ioc.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"

    xmlns:context="http://www.springframework.org/schema/context"

    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">

    <bean id="student" class="com.my.test.Student">  

        <property name="name" value="Lucky"/>  

        <property name="teacher" ref="teacher"/>  

    </bean>  

    <bean id="teacher" class="com.my.test.Teacher">  

        <property name="name" value="Sheryl"/>  

    </bean>  

</beans>  

2.3 類代碼:Student類,Teacher類

(1)Student類

package com.my.test;

public class Student {

    private String name;

    private Teacher teacher;

    public String getName() {

        return this.name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public Teacher getTeacher() {

        return this.teacher;

    }

    public void setTeacher(Teacher teacher) {

        this.teacher = teacher;

    }

}

(2)Teacher類

package com.my.test;

public class Teacher {

    private String name;

    public String getName() {

        return this.name;

    }

    public void setName(String name) {

        this.name = name;

    }

}

2.4 測試類(程式入口)

package com.my.test.demo;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.my.test.Student;

import com.my.test.Teacher;

public class SpringIoCDemoTest {

    public static void main(String[] args) {

        //跟spring要執行個體,補充:spring預設是單例模式

        ApplicationContext context = new ClassPathXmlApplicationContext("bean_ioc.xml");

        Student stu1 = (Student) context.getBean("student");

        Student stu2 = (Student) context.getBean("student");

        Teacher teacher = (Teacher) context.getBean("teacher");

        //下方兩個語句輸出結果一樣。在Student的執行個體中,引用了Teacher執行個體,由spring來做

        System.out.println("學生1姓名是:" + stu1.getName() + ", 老師是:" + stu1.getTeacher().getName());

        System.out.println("學生1姓名是:" + stu2.getName() + ", 老師是:" + stu2.getTeacher().getName());

        //直接列印Teacher的執行個體,同樣是由spring來生成

        System.out.println("老師姓名是:" + teacher.getName());

    }

}

輸出結果:

學生1姓名是:Lucky, 老師是:Sheryl

學生1姓名是:Lucky, 老師是:Sheryl

老師姓名是:Sheryl