Spring产生历史

Spring

Java 领域第一框架,是行业标准和规范。

Java EE 基于 Java 的企业级解决方案,Java WEB 开发就是 Java EE 的一部分。
Java 初期使用 EJB 开发,但是这种方式非常繁重,不灵活,不便于维护和升级。
//创建Class三种方式
//1
Class clazz1 = Student.class;
//2
Class clazz2 = null;
try {
    clazz2 = Class.forName("com.m.ioc.entity.Student");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
System.out.println(clazz1 == clazz2);   //true
//3
Student student = new Student();
Class clazz3 = student.getClass();
System.out.println(clazz2 == clazz3);   //true

Spring IoC 的雏形,读取配置文件的方式,通过反射机制自动创建开发中需要使用的组件对象,并且完成依赖注入,对象和对象之间的联系。


2002 正式发布 Spring 1.0 版本,发展至今,将近 20 年的发展,Spring 框架已经从最初取代 EJB 构建企业级开发的方式,发展成一套自有的生态体系。


SSH:Structs2(实现 MVC,客户端和服务器交互)、Spring(构建项目)、Hibernate(持久层)

SSM:Spring MVC、Spring、MyBatis
Spring 已经发展成了一套生态体系,常规意义上的 Spring 框架是指 Spring Framework,

Spring Framework 是整套 Spring 全家桶所有产品的基石,

Spring 全家桶的所有组件产品都是基于 Spring Framework,IoC 容器。

IoC

控制反转具体反转的是 Java 程序中对象的创建方式,传统开发方式,对象都是开发者需要的时候手动 new 出来的。

控制反转是对象不需要开发者手动 new,而是交给 IoC 容器来创建,开发者直接使用即可。

1、创建 Java 工程,导入 Spring 的依赖(jar)

Spring IoC 的使用流程

1、将程序中需要用到的对象在 spring.xml 中进行配置。

2、读取 spring.xml 生成 ApplicationContext 对象(IoC 容器)。

3、从 ApplicationContext 对象中获取需要的 bean

- 通过 id 获取(必须保证 IoC 中的每个 bean 都是不同的 id)
- 通过 Class 类型获取(必须保证该类的 bean 只有一个)


1、通过无参构造创建对象,通过 setter 方法完成属性的赋值。

2、通过有参构造创建对象,同时完成赋值。
<bean id="stu2" class="com.southwind.entity.Student">
    <constructor-arg name="id" value="2"></constructor-arg>
    <constructor-arg name="name" value="李四"></constructor-arg>
    <constructor-arg name="age" value="23"></constructor-arg>
</bean>
<bean id="stu2" class="com.southwind.entity.Student">
    <constructor-arg index="1" value="李四"></constructor-arg>
    <constructor-arg index="0" value="2"></constructor-arg>
    <constructor-arg index="2" value="23"></constructor-arg>
</bean>

//bean 之间的依赖注入(DI),IoC 包含 DI,IoC 可以自动完成 DI 的操作,把一个 bean 注入到另外一个 bean。

传统方式

package com.southwind.test;

import com.southwind.entity.Class;
import com.southwind.entity.Student;

public class Test {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1);
        student.setName("张三");
        student.setAge(22);
        Class clazz = new Class();
        clazz.setId(101);
        clazz.setName("一班");
        //依赖注入 DI
        student.setClazz(clazz);
    }
}

IoC

<!-- 通过无参创建对象 -->
<bean id="stu" class="com.southwind.entity.Student">
    <property name="id" value="1"></property>
    <property name="name" value="李四"></property>
    <property name="age" value="22"></property>
    <property name="clazz" ref="clazz"></property>
</bean>

<!-- Class对象 -->
<bean id="clazz" class="com.southwind.entity.Class">
    <property name="id" value="101"></property>
    <property name="name" value="一班"></property>
</bean>

//两个 bean 之间的 DI,一定要使用 ref 完成注入。

传统方式

package com.southwind.test;

import com.southwind.entity.Class;
import com.southwind.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setId(1);
        stu1.setName("张三");
        stu1.setAge(22);
        Student stu2 = new Student();
        stu2.setId(2);
        stu2.setName("李四");
        stu2.setAge(23);
        Class clazz = new Class();
        clazz.setId(101);
        clazz.setName("一班");
        List<Student> list = new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        clazz.setStudents(list);
        System.out.println(clazz);
    }
}

IoC

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="stu1" class="com.southwind.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="22"></property>
    </bean>

    <bean id="stu2" class="com.southwind.entity.Student">
        <property name="id" value="2"></property>
        <property name="name" value="李四"></property>
        <property name="age" value="23"></property>
    </bean>

    <bean id="clazz" class="com.southwind.entity.Class">
        <property name="id" value="101"></property>
        <property name="name" value="一班"></property>
        <property name="students">
            <list>
                <ref bean="stu1"></ref>
                <ref bean="stu2"></ref>
            </list>
        </property>
    </bean>

</beans>
原文地址:https://www.cnblogs.com/k-class/p/13963041.html