框架体系——Spring
创始人
2024-02-18 15:28:40
0

Spring IOC

IOC控制反转

  • IOC 控制反转,全称Inverse of Control,是一种设计理念
  • 由代理人来创建和管理对象,消费者通过代理人来获取对象
  • Ioc的目的是降低对象之间的耦合
  • 通过加入Ioc容器将对象统一管理,将对象关联变为弱耦合。
    在这里插入图片描述

DI依赖注入

  • IoC是设计理念,是现代程序设计遵循的标准,是宏观目标
  • DI(Dependency Injection)是具体技术实现,是微观实现。
  • DI在java中就是利用反射技术实现对象注入(Injection)

Spring

  • Spring可以从狭义和广义两个角度看待
  • 狭义的Spring是指Spring框架(Spring Framework)
  • 广义的Spring是指Spring生态体系

狭义的Spring框架

  • Spring框架是企业开发复杂性的一站式解决方案
  • Spring框架的核心是IoC容器与AoP面向切面编程
  • Spring IoC负责创建与管理系统对象,并在此基础上拓展功能

广义的Spring框架

Spring 全家桶,包括Spring data,springboot,springcloud等等

传统开发方式

  • 对象直接引用导致对象硬性关联,程序难以维护拓展
  • 在这里插入图片描述

Spring IoC容器

  • IoC容器是Spring生态的地基,用于统一创建与管理对象依赖
    在这里插入图片描述
    Spring IoC容器职责
  • 对象的控制权交由第三方统一管理
  • 利用Java反射技术实现运行时对象创建与关联(DI依赖注入)
  • 基于配置提高应用程序的可维护性与拓展性

Spring IoC初体验

需求
在这里插入图片描述
下面是普通的代码实现,将child和apple进行强关联,这就出现了一个问题,灵活性不高,如果我想修改,就必须改动源代码

    public static void main(String[] args) {Apple apple1 = new Apple("红富士", "红色", "欧洲");Apple apple2 = new Apple("绿富士", "绿色", "绿大利");Apple apple3 = new Apple("蓝富士", "蓝色", "兰博基尼");Child lily = new Child("lily", apple1);Child andy = new Child("andy", apple2);Child luna = new Child("luna", apple3);lily.eat();andy.eat();luna.eat();}

针对这个问题,Spring应运而生,下面我们使用Spring来实现上述逻辑

Spring
首先,引入Spring的依赖

 org.springframeworkspring-context4.3.11.RELEASE

下面是几大重要的包
在这里插入图片描述

接着在resources下新建applicationContext.xml,增加配置如下:







然后新建SpringApplication类,看看获取对象

package com.imooc.imooc.spring.ioc.eneity;import com.imooc.imooc.spring.ioc.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** todo {类简要说明}** @Author wangw* @Date 2022/11/22 19:34* @Version 1.0*/
public class SpringApplication {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("classpath:applicationContext.xml");Apple sweetApple =  context.getBean("sweetApple",Apple.class);System.out.println(sweetApple.getTitle());}
}

配置Bean的三种方式

Spring框架有三种配置Bean的方式,分别是 XML配置Bean)基于注解配置Bean基于Java代码配置Bean
下面我们详细学习如何通过XML配置Bean;

实例化bean的三种方法

而使用XML配置bean,也有三种实例化Bean方法 基于构造方法对象实例化、基于静态工厂实例化、基于工厂实例方法实例化
在这里插入图片描述

下面是通过工厂类实例化对象,优势在于隐藏了对象创建的细节。
首先是配置:

 

然后是两个工厂及其方法:

public class ApplyFactoryInstance {public Apple createSweetApple(){Apple apple =new Apple();apple.setOrigin("欧洲");apple.setColor("红色");apple.setTitle("红富士");return apple;}
}
public class AppleStaticFactory {public static Apple CreateSweetApple(){Apple apple =new Apple();apple.setOrigin("欧洲");apple.setColor("红色");apple.setTitle("红富士");return apple;}
}

初始化完成后,我们如何从Ioc容器中获取bean呢?有两种方式,分别如下:
在这里插入图片描述

另外,其实在xml中 bean有IDName两个属性,这两个属性有什么区别呢?
首先,我们来看看他们的相同点:

  • bean id 和name都是设置对象在IoC容器中的唯一标识
  • 两者在同一个配置文件中都不允许重复
  • 两者允许在多个配置文件中出现重复,新对象覆盖旧对象

两者不同点:

  • id要求更为严格,一次只能定义一个对象标识(推荐)
  • name更为宽松,一次允许定义多个对象标识
  • tips:id和name命名要求有意义,且驼峰命名
    除此之外,其实Spring还支持无ID/name属性,此时使用类名全路径作为唯一标识。

对象依赖注入

  • 依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作
  • 基于Setter注入对象
  • 基于构造方法注入对象

IOC在项目中的重要用途

示例代码如下:
在这里插入图片描述

ApplicationContext-service.xml:




ApplicationContext-dao.xml:




BookDao

package com.imooc.spring.ioc.bookshop.dao;/*** todo {类简要说明}** @Author wangw* @Date 2022/11/24 22:42* @Version 1.0*/
public interface BookDao {public void insert();
}

BookDaoImpl

package com.imooc.spring.ioc.bookshop.dao;/*** todo {类简要说明}** @Author wangw* @Date 2022/11/24 22:42* @Version 1.0*/
public class BookDaoImpl implements BookDao{public void insert() {System.out.println("向Mysql Book 表插入一条数据");}
}

bookShopApplication

package com.imooc.spring.ioc.bookshop;import com.imooc.spring.ioc.bookshop.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** todo {类简要说明}** @Author wangw* @Date 2022/11/24 22:47* @Version 1.0*/
public class bookShopApplication {public static void main(String[] args) {ApplicationContext context =  new ClassPathXmlApplicationContext("classpath:ApplicationContext-*.xml");BookService bookService = context.getBean("bookService",BookService.class);bookService.purchase();}
}

实现对象依赖注入有两种方式:setter方法注入/构造方法注入

区别在于xml‘中bean参数 一个使用 property 一个使用 constructor-arg

注入集合对象

1. 注入List:
在这里插入图片描述

2. 注入set
在这里插入图片描述

3. 注入map
在这里插入图片描述

  1. 注入Properties
    在这里插入图片描述

示例代码:



2001-总裁办2003-总经理办公室2010-研发部会议室2010-研发部会议室010-12345678湖北省武汉市xx中心http:www.baidu.com

查看容器内对象

示例代码:

package com.imooc.spring.ioc;import com.imooc.spring.ioc.entity.Company;
import com.imooc.spring.ioc.entity.Computer;
import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** todo {类简要说明}** @Author wangw* @Date 2022/11/24 23:09* @Version 1.0*/
public class SpringApplication {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");Company company = context.getBean("company", Company.class);System.out.println(company);System.out.println(company.getInfo().getProperty("website"));// 获取容器内对象名称String[] beanNames = context.getBeanDefinitionNames();for (String beanName : beanNames) {System.out.println(beanName);System.out.println("类型:"+context.getBean(beanName).getClass().getName());System.out.println("内容:"+context.getBean(beanName).toString());}}
}

bean scope属性

  • bean scope 属性用于决定对象核实被创建
  • bean scope 配置将影响容器内对象的数量
  • 默认情况下bean会在IoC容器创建后自动序列化,全局唯一

bean scope属性清单

在这里插入图片描述

singleton 与 prototype对比

在这里插入图片描述

bean的生命周期

在这里插入图片描述
配置示例:
在这里插入图片描述

实现极简IoC容器(模拟Spring实现流程)

首先是IOC容器类:
接口:

package com.imooc.spring.ioc.context;/*** ApplicationContext 接口** @Author wangw* @Date 2022/11/26 22:15* @Version 1.0*/
public interface ApplicationContext {public Object getBean(String beanId);
}

实现类:

package com.imooc.spring.ioc.context;import com.imooc.spring.ioc.entity.Apple;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** ApplicationContext 实现类,本质就是一个IOC容器** @Author wangw* @Date 2022/11/26 22:16* @Version 1.0*/
public class ClassPathXmlApplicationContext implements ApplicationContext {private Map iocContainer = new HashMap();public ClassPathXmlApplicationContext() {try {String filePath = this.getClass().getResource("/applicationContext.xml").getPath();filePath = new URLDecoder().decode(filePath, "UTF-8");SAXReader reader = new SAXReader();Document document = reader.read(filePath);List nodes = document.getRootElement().selectNodes("bean");for (Node node : nodes) {Element element = (Element) node;String id = element.attributeValue("id");String className = element.attributeValue("class");Class class1 = Class.forName(className);Object obj = class1.newInstance();List list = element.selectNodes("property");for (Node node1 : list) {Element property = (Element) node1;String propName = property.attributeValue("name");String propValue = property.attributeValue("value");String setMethodName ="set"+propName.substring(0,1).toUpperCase()+propName.substring(1);System.out.println("准备执行"+setMethodName+"方法注入数据");Method method = class1.getMethod(setMethodName,String.class);method.invoke(obj,propValue);}iocContainer.put(id, obj);System.out.println("ioc容器初始化完毕");System.out.println(iocContainer);}} catch (Exception e) {e.printStackTrace();}}public Object getBean(String beanId) {return iocContainer.get(beanId);}
}

配置XML:




实体类:

package com.imooc.spring.ioc.entity;/*** todo {类简要说明}** @Author wangw* @Date 2022/11/26 22:11* @Version 1.0*/
public class Apple {private String title;private String color;private String origin;public Apple() {}public Apple(String title, String color, String origin) {this.title = title;this.color = color;this.origin = origin;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getOrigin() {return origin;}public void setOrigin(String origin) {this.origin = origin;}
}

启动类:

package com.imooc.spring.ioc;import com.imooc.spring.ioc.context.ApplicationContext;
import com.imooc.spring.ioc.context.ClassPathXmlApplicationContext;
import com.imooc.spring.ioc.entity.Apple;/*** todo {类简要说明}** @Author wangw* @Date 2022/11/26 22:27* @Version 1.0*/
public class Application {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext();Apple apple = (Apple) context.getBean("sweetApple");System.out.println(apple);}
}

至此实现效果如下,可以看出其实Spring IoC容器就是通过反射实现的
在这里插入图片描述

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...