IoC和DI

Spring 是包含众多工具的 IoC 容器,存的是对象,对象这个词在 Spring 的范围内,称之为 bean

IoC 是控制反转

控制权进行了反转,比如对某一个东西的控制权在 A 手上,结果变成了 B ,Spring 管理的是 bean ,所以这里的控制权指的是 bean 的控制权,也就是对象的控制权进行了反转

之前我们依赖的一些对象,我们需要自己去创建,通过 new 来进行创建,有了 Spring 之后我们就不再进行创建了,而是由 Spring 帮助我们进行创建,这就叫控制反转

控制反转的一个优点主要是解耦

耦合度高就说明两个事务的关联性密切,我们项目开发软件设计的原则是高内聚,低耦合

高内聚 : 是指一个模块内部的关系(比如一个班级)

低耦合 : 是指各个模块之间的关系(比如各个班级)

高内聚低耦合就是,一个班级内需要团结协作,但是各个班级之间的相互影响要小,一个班级的问题对别的班级的影响要降低

Spring MVC 和 Spring Boot 都属于 Spring ,Spring MVC 是基于 Spring 的一个 MVC 框架 ,而 Spring Boot 是基于 Spring 的一套快速开发整合包

IoC 是思想, DI(依赖注入) 是是一种实现方式

IoC 就是依赖的象的创建的控制权交给了 Spring 进行管理(存对象), DI 就是如何将依赖对象取出来,并赋给该对象的属性(取对象)

IoC 提供了两类注解 : 

1.类注解 : @Controller(控制器存储) , @Service(服务存储) , @Repository(仓库存储) , @Component(组件存储) , @Configuration(配置存储)

2.方法注解 : @Bean

接下来我们使用一下这些注解

①  @Controller

先创建一个 controller 包,在里面创建一个 UserController 类,用 @Controller 告诉 Spring 帮我们管理这个对象

package com.example.ioc.controller;

import org.springframework.stereotype.Controller;

//用 @Controller 告诉 Spring 帮我们管理这个对象
@Controller
public class UserController {
    public void doController(){
        System.out.println("do Controller...");
    }
}

然后在启动类 DemoApplication 中查看 Spring 有没有把上面这个 Controller 存进来呢

package com.example.ioc;
import com.example.ioc.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //Spring 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        //返回的就是 Spring 的运行环境

        UserController bean = context.getBean(UserController.class);
        bean.doController();
    }

}

 运行出来了,说明存进去了

 IoC和DI_第1张图片

bean 一般使用这三个方法IoC和DI_第2张图片

②  @Service

package com.example.ioc.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void doService(){
        System.out.println("do service....");
    }
}

依旧在启动类 DemoApplication 中查看

package com.example.ioc;

import com.example.ioc.controller.UserController;
import com.example.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //Spring 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        //返回的就是 Spring 的运行环境

        UserController bean = context.getBean(UserController.class);
        bean.doController();

        UserService userService = context.getBean(UserService.class);
        userService.doService();
    }

}

成功拿到 

IoC和DI_第3张图片

根据名称来获取 UserService 的 bean

(名称为类名转换为小驼峰,特殊情况就是如果类名前两位都是大写,那么bean的名称就是类名)

package com.example.ioc;

import com.example.ioc.controller.UserController;
import com.example.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //Spring 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        //返回的就是 Spring 的运行环境

        UserController bean = context.getBean(UserController.class);
        bean.doController();

        UserService userService = context.getBean(UserService.class);
        userService.doService();

        //根据名称来获取bean
        UserService userService1 = (UserService) context.getBean("userService");
        userService1.doService();
    }

IoC和DI_第4张图片

根据名称+类型来拿 bean

package com.example.ioc;

import com.example.ioc.controller.UserController;
import com.example.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //Spring 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        //返回的就是 Spring 的运行环境

        UserController bean = context.getBean(UserController.class);
        bean.doController();

        UserService userService = context.getBean(UserService.class);
        userService.doService();

        //根据名称来获取bean
        UserService userService1 = (UserService) context.getBean("userService");
        userService1.doService();

        //根据名称和类型来获取 bean
        UserService userService2 = context.getBean("userService",UserService.class);
        userService2.doService();
    }

IoC和DI_第5张图片

IoC和DI_第6张图片

③  @Repository

package com.example.ioc.repo;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
    public void doRepository(){
        System.out.println("do repo...");
    }
}
package com.example.ioc;

import com.example.ioc.controller.UserController;
import com.example.ioc.repo.UserRepository;
import com.example.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //Spring 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        //返回的就是 Spring 的运行环境

        UserController bean = context.getBean(UserController.class);
        bean.doController();

        UserService userService = context.getBean(UserService.class);
        userService.doService();

        //根据名称来获取bean
        UserService userService1 = (UserService) context.getBean("userService");
        userService1.doService();

        //根据名称和类型来获取 bean
        UserService userService2 = context.getBean("userService",UserService.class);
        userService2.doService();

        UserRepository userRepository = context.getBean(UserRepository.class);
        userRepository.doRepository();
    }

IoC和DI_第7张图片

④  @Component

package com.example.ioc.component;

import org.springframework.stereotype.Component;

@Component
public class UserComponent {
    public void doComponent(){
        System.out.println("do component");
    }
}
package com.example.ioc;

import com.example.ioc.component.UserComponent;
import com.example.ioc.controller.UserController;
import com.example.ioc.repo.UserRepository;
import com.example.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //Spring 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        //返回的就是 Spring 的运行环境

        UserController bean = context.getBean(UserController.class);
        bean.doController();

        UserService userService = context.getBean(UserService.class);
        userService.doService();

        //根据名称来获取bean
        UserService userService1 = (UserService) context.getBean("userService");
        userService1.doService();

        //根据名称和类型来获取 bean
        UserService userService2 = context.getBean("userService",UserService.class);
        userService2.doService();

        UserRepository userRepository = context.getBean(UserRepository.class);
        userRepository.doRepository();

        UserComponent userComponent = context.getBean(UserComponent.class);
        userComponent.doComponent();
    }

}

IoC和DI_第8张图片

⑤  @Configuration

package com.example.ioc.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class UserConfig {
    public void doConfig(){
        System.out.println("do configurarion...");
    }
}
package com.example.ioc;

import com.example.ioc.component.UserComponent;
import com.example.ioc.config.UserConfig;
import com.example.ioc.controller.UserController;
import com.example.ioc.repo.UserRepository;
import com.example.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //Spring 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        //返回的就是 Spring 的运行环境

        UserController bean = context.getBean(UserController.class);
        bean.doController();

        UserService userService = context.getBean(UserService.class);
        userService.doService();

        //根据名称来获取bean
        UserService userService1 = (UserService) context.getBean("userService");
        userService1.doService();

        //根据名称和类型来获取 bean
        UserService userService2 = context.getBean("userService",UserService.class);
        userService2.doService();

        UserRepository userRepository = context.getBean(UserRepository.class);
        userRepository.doRepository();

        UserComponent userComponent = context.getBean(UserComponent.class);
        userComponent.doComponent();

        UserConfig userConfig = context.getBean(UserConfig.class);
        userConfig.doConfig();
    }

}

IoC和DI_第9张图片

IoC和DI_第10张图片

你可能感兴趣的:(java,开发语言)