软件定制开发供应商Spring | 一文带你掌握IOC技术

👑 博主简介
   🥇
   🥇
🤝 交流社区

文章目录

IOC

1、概念

IOC(Inversion of Control):控制反转

  • 软件定制开发供应商什么是控制反转呢?

软件定制开发供应商使用对象时,由主动new软件定制开发供应商产生对象转换为由外部提供对象,软件定制开发供应商此过程中对象创建控制软件定制开发供应商权由程序转移到外部,软件定制开发供应商此思想称为控制反转

  • Spring和IOC软件定制开发供应商之间的关系是什么呢?

Spring技术对IOC思想进行了实现,Spring提供了一个容器,称为IOC容器,用来充当IOC思想中的"外部",IOC思想中的"外部"指的就是Spring的IOC容器

  • IOC容器的作用以及内部存放的是什么?

IOC容器负责对象的创建、初始化等一系列工作,其中包含了数据层和业务层的类对象,被创建或被管理的对象在IOC容器中统称为Bean,IOC容器中放的就是一个个的Bean对象

2、分析

  • Spring是使用容器来管理bean对象的,那么管什么?

主要管理项目中所使用到的类对象,比如(Service和Dao)

  • 如何将被管理的对象告知IOC容器?

使用配置文件

  • 被管理的对象交给IOC容器,要想从容器中获取对象,就先得思考如何获取到IOC容器?

Spring框架提供相应的接口

  • IOC容器得到后,如何从容器中获取bean?

调用Spring框架提供对应接口中的方法

  • 使用Spring导入哪些坐标?

用别人的东西,就需要在pom.xml添加对应的依赖

3、IOC代码实现

3.1、

3.2、添加Spring的依赖jar包

  • pom.xml
<dependencies>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-context</artifactId>        <version>5.2.10.RELEASE</version>    </dependency>    <dependency>        <groupId>junit</groupId>        <artifactId>junit</artifactId>        <version>4.12</version>        <scope>test</scope>    </dependency></dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.3、创建和Service层的接口与实现类

  • BookDao
public interface BookDao {    public void save();}
  • 1
  • 2
  • 3
  • BookDaoImpl
public class BookDaoImpl implements BookDao {    public void save() {        System.out.println("book dao save ...");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • BookService
public interface BookService {    public void save();}
  • 1
  • 2
  • 3
  • BookServiceImpl
public class BookServiceImpl implements BookService {    private BookDao bookDao = new BookDaoImpl();    public void save() {        System.out.println("book service save ...");        bookDao.save();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.4、添加spring配置文件

resources下添加spring配置文件applicationContext.xml,并配置Bean

  • bean标签 标示配置bean
  • id属性 标示给bean起名字
  • class属性 表示给bean定义类型

配置应用程序上下文

  • 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="bookDao" class="com.bby.dao.BookDaoImpl"/>        <bean id="bookService" class="com.bby.service.BookServiceImpl"/></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意:bean定义时id属性在同一个上下文中(配置文件)不能重复

3.5、获取IOC容器

使用Spring提供的接口完成IOC容器的创建,创建App类,编写main方法

public class App {    public static void main(String[] args) {        //获取IOC容器		 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");           }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.6、从容器中获取对象进行方法调用

public class App {    public static void main(String[] args) {        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");        BookDao bookDao = (BookDao)applicationContext.getBean("bookDao");        bookDao.save();        System.out.println("---------------------------");        BookService bookService = (BookService)applicationContext.getBean("bookService");        bookService.save();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.7、运行程序测试

思考:我们虽然从IOC容器中获取到了Bean,但是这样是我们真正想要的效果吗?由于我们在代码中还是使用了new来创建对象,所以代码的耦合性还是很高,我们想要解耦,就要在用的时候就从IOC容器中获取,而不是自己new出来,但是service对象依赖于dao对象,这种依赖关系如何告知IOC容器呢?这时候就需要我们下面所讲的DI技术了。

DI 依赖注入

1、概念

DI(Dependency Injection):依赖注入

  • 当IOC容器中创建好service和dao对象后,程序能正确执行吗?

不行,因为service运行需要依赖dao对象,IOC容器中虽然有service和dao对象,但是service对象和dao对象没有任何关系,需要把dao对象交给service,也就是说要绑定service和dao对象之间的关系,像这种在容器中建立对象与对象之间的绑定关系就要用到DI

  • 什么是依赖注入?

在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入

  • IOC容器中哪些bean之间要建立依赖关系?

这个需要程序员根据业务需求提前建立好关系,如业务层需要依赖数据层,service就要和dao建立依赖关系

2、分析

  • 要想实现依赖注入,必须要基于IOC管理Bean

DI的入门案例要依赖于前面IOC的入门案例

  • Service中使用new形式创建的Dao对象是否保留?

需要删除掉,最终要使用IOC容器中的bean对象

  • Service中需要的Dao对象如何进入到Service中?

在Service中提供方法,让Spring的IOC容器可以通过该方法传入bean对象

  • Service与Dao间的关系如何描述?

使用配置文件

3、DI代码实现

3.1、去除代码中的new

  • 在BookServiceImpl类中,删除业务层中使用new的方式创建的dao对象
public class BookServiceImpl implements BookService {    //删除业务层中使用new的方式创建的dao对象    private BookDao bookDao;    public void save() {        System.out.println("book service save ...");        bookDao.save();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.2、为属性提供setter方法

  • 在BookServiceImpl类中,为BookDao提供setter方法
//提供对应的set方法public void setBookDao(BookDao bookDao) {    this.bookDao = bookDao;}
  • 1
  • 2
  • 3
  • 4

3.3、修改配置完成注入

<?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="bookDao" class="com.bby.dao.BookDaoImpl"/>    <bean id="bookService" class="com.bby.service.BookServiceImpl">        <!--配置server与dao的关系-->        <!--property标签表示配置当前bean的属性        		name属性表示配置哪一个具体的属性        		ref属性表示参照哪一个bean		-->        <property name="bookDao" ref="bookDao"/>    </bean></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.4、运行程序测试

这样我们就更好的实现了IOC与DI的目标:充分解耦

尾言:创作不易,如果本文的内容对您有帮助,还望客官可以支持一下博主,👍(点赞)+✏️(评论)+⭐️(收藏)是我创作的巨大动力!

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发