其他分享
首页 > 其他分享> > Spring 01: Spring配置 + IOC控制反转 + Setter注入

Spring 01: Spring配置 + IOC控制反转 + Setter注入

作者:互联网

简介

Spring框架是一个容器,是整合其他框架的框架
他的核心是IOC(控制反转)和AOP(面向切面编程),由20多个模块构成,在很多领域都提供了优秀的问题解决方案

特点

image

示例分析:在开发中普遍需要使用到日志输出功能,会将日志输出功能大量耦合到项目的不同位置,如上图左侧所示。
而日志输出功能与项目本身的核心业务逻辑无关,我们只是为了不时的查看程序的运行状态。则可以将日志功能单独提出去开发,在需要的地方将日志输出功能(所谓:切面)反织回去即可,如上图右侧所示。

IOC

//程序员创建对象
Student stu = new Student();
//程序员进行赋值
stu.setName("荷包蛋");		
stu.setAge(20);
<!-- Spring容器负责对象的创建 -->
<bean id="stu" class="com.example.pojo.Student">
    <!-- Spring容器负责为属性赋值 -->
    <property name="name" value="荷包蛋" />
    <property name="age" value="20" />
</bean>

创建Spring项目

    <!-- 添加spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.22</version>
    </dependency>

image

image

创建实例对象

//实体类
package com.example.pojo;

public class Student {
    private String name;
    private int age;

    public Student() {
        System.out.println("无参方法被调用,Student实例被创建.....");
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
<!-- applicationContext.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="stu" class="com.example.pojo.Student"/>
</beans>
//两种创建实例对象的对比

package com.example.test;

import com.example.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudent {

    //测试程序员手动创建实例对象
    @Test
    public void testStudent(){
        Student stu = new Student();
        System.out.println(stu);
    }

    //测试Spring容器创建实例对象
    @Test
    public void testStudentSpring(){
        //创建Spring容器,并启动
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取对象
        Student stu = (Student) applicationContext.getBean("stu");
        System.out.println(stu);
    }
}
    //两次测试的输出结果均为

    无参方法被调用,Student实例被创建.....
    Student{name='null', age=0}

    Process finished with exit code 0
    @Test
    public void testStudentSpring(){
        //创建Spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("source01/applicationContext.xml");
    }
    //测试输出结果

    无参方法被调用,Student实例被创建.....

    Process finished with exit code 0

基于xml的IOC(控制反转)

setter注入

1. 简单类型(8 + 1)注入

    //添加两个属性的set方法
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
    <bean id="stu" class="com.example.pojo.Student">
        <!-- 使用setter注入法为实例对象的属性赋值 -->
        <property name="name" value="荷包蛋"/>
        <property name="age" value="20"/>
    </bean>
    //测试Spring容器创建实例对象
    @Test
    public void testStudentSpring(){
        //创建Spring容器,并启动
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("source01/applicationContext.xml");
        //从容器中获取对象
        Student stu = (Student) applicationContext.getBean("stu");
        System.out.println(stu);
    }
    //测试输出:
    无参方法被调用,Student实例被创建.....
    Student{name='荷包蛋', age=20}

    Process finished with exit code 0

2. 引用类型注入

    //School类
    private String name;
    private String address;

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public School() {
        System.out.println("School类的构造方法被执行,实体对象被创建.....");
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
    //Student类
    private String name;
    private int age;
    private School school;

    public Student() {
        System.out.println("Student类的构造方法执行,实体对象被创建....");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
    <!-- applicationContext.xml文件 --> 

   <!-- 定义School实体类的实例对象-->
    <bean id="school" class="com.example.pojo02.School">
        <property name="name" value="nefu"/>
        <property name="address" value="哈尔滨"/>
    </bean>

    <!-- 定义Student实体类的实例对象 -->
    <bean id="stu" class="com.example.pojo02.Student">
        <property name="name" value="荷包蛋"/>
        <property name="age" value="20"/>
        <!-- 根据bean工厂中注册过的对象,进行依赖注入 -->
        <property name="school" ref="school"/>
    </bean>
     //测试setter注入法的ref属性
     @Test
     public void testStudent(){
         //创建Spring容器,同时生成bean工厂中注册的对象
         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("source02/applicationContext.xml");
         //获取对象
         Student stu = (Student) applicationContext.getBean("stu");
         System.out.println(stu);
     }
     //测试输出结果
     School类的构造方法被执行,实体对象被创建.....
     Student类的构造方法执行,实体对象被创建....
     Student{name='荷包蛋', age=20, school=School{name='nefu', address='哈尔滨'}}
 
     Process finished with exit code 0

注意

标签:01,name,Spring,age,Student,创建,IOC,public
来源: https://www.cnblogs.com/nefu-wangxun/p/16600655.html