• 【Spring IOC/DI】bean 的 5 种注册 与 5 种注入


    什么是 bean

    一个 bean 就是一个实例化对象

    User user = new User()

    上面这行代码中的 user, 就是 User 类的实例化对象,即一个 bean(User Bean)

    什么是 IOC

    Inversion of Control 控制反转(反转对 bean 的控制)

     spring ioc 是一个 bean 容器,我们可以将需要的 bean 放入其中

    我创建我使用 变为 她创建我使用

    使用 UserController 调用 UserService

    我在 UserController 中 new UserService() ,我调用 userService 中的方法

    她为我提供一个 new 好的 UserService 对象,我调用 userService 中的方法

    我无需在意何时创建对象,何时使用对象,何时销毁对象(她来管理对象的生命周期)

    我无需使用构造器 new 对象,不用关注构造器需要的参数(当构造器参数变化时,我也无需知道,我只是调用已经创建好的对象中的方法而已) 

    5 种将 bean 注册进 SpringIOC 容器的方式

    注册 bean 的方式分为 2 大类 5 小类

    大类小类介绍

    XML

    注册

            class

    />

    直接类名注册

            class

            fatory-method

    />

    静态方法注册

            factory-bean

            fatory-method

    />

    实例方法注册

    @

    注册

    @Configuration @Bean

    Java-Based 注册

    允许以编程方式配置 bean

    @Component

    @Controller

    @Service @Repository

    Annotation-Based 注册

    将使用这类注解的类

    对应的实例化对象

    注册进 IOC 容器

    XML注册

    直接类名注册

    1. <bean id="userService"
    2. class="com.syt.UserService"
    3. />

    静态工厂方法注册

    1. <bean id="staticUserService"
    2. class="com.syt.UserServiceStaticFactory"
    3. factory-method="staticUserService"
    4. />
    1. public class UserStaticFactory {
    2. public static UserService staticUserService() {
    3. return new UserService();
    4. }
    5. }

    实例工厂方法注册

    1. <bean id="userServiceIntanceFactory"
    2. class="com.syt.UserServiceIntanceFactory"
    3. />
    4. <bean id="instanceUserService"
    5. factory-bean="userServiceIntanceFactory"
    6. factory-method="instanceUserService"
    7. />
    1. public class UserServiceIntanceFactory {
    2. public User intanceUserService() {
    3. return new UserService();
    4. }
    5. }

    属性初始值设置

    1. <bean>
    2. <property
    3. name
    4. value
    5. />
    6. bean>
    @ 注册

    Annotation-Based

    1. @Component
    2. public class UserService {
    3. }

    Java-Based

    1. @Configuration
    2. public class UserServiceConfiguration {
    3. @Bean
    4. public UserService userService() {
    5. // 这里可以对 UserService
    6. // 进行配置后
    7. // 再返回配置好的 UserService 对象
    8. return new UserService();
    9. }
    10. }

     使用 spring 进行 @ 注册 需要在 beans 配置中配置自动包扫描路径

    1. <context:component-scan
    2. base-pakage
    3. />

     什么是 DI

    Dependency Injection 依赖注入(将 bean 注入进依赖 bean 使用 bean 的类) 

    获取 IOC 容器中的 bean 的方式

    5 种将 bean 注入进我们自己的代码中的方式

    注入 bean 的方式分为 3 大类 5 小类

    大类小类介绍
    属性注入@Autowired / @Resouce

    在代码中添加对应 bean 属性

    在 bean 属性上添加这类注解

    Setter 注入@Autowired / @Resouce

    在代码中添加对应 bean 属性

    并且实现对应 setter 方法

    在 setter 方法上添加这类注解

            class

    >

            

                    name

                    ref

            />

    使用 XML 在注册的同时注入

    ,也需要注册类中有

    bean 属性 与 对应 setter 方法

    构造方法注入@Autowired / @Resouce

    在代码中添加对应 bean 属性

    并且实现对应构造方法

    在构造方法上添加这类注解

            class

    >

            

                    name

                    ref

            />

    使用 XML 在注册的同时注入

    ,也需要注册类中有

    bean 属性 与 对应 构造方法

    属性注入
    1. public class UserController {
    2. @Autowired
    3. private UserService userService;
    4. }
    Setter 注入
    1. public class UserController {
    2. private UserService userService;
    3. @Autowired
    4. public void SetUserService(UserService userService) {
    5. this.userService = userService;
    6. }
    7. }
    1. <bean id="propertyUserService"
    2. class="com.syt.service.UserService"
    3. >
    4. <property
    5. name="userDao"
    6. ref="userDao"
    7. />
    8. bean>
    构造方法注入
    1. public class UserController {
    2. private UserService userService;
    3. @Autowired
    4. public UserController (UserService userService) {
    5. this.userService = userService;
    6. }
    7. }
    1. <bean id="constructorUserService"
    2. class="com.syt.service.UserService">
    3. <constructor-arg
    4. name="userDao"
    5. ref="userDao"
    6. />
    7. bean>

    补充

    beans xml 配置

    位置:

    一般位于 resources 下,命名为 spring-config.xml 或 applicationContext.xml

    基本内容

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:content="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. beans>
    spring-context 依赖

    我用的 JDK 为 1.8

    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-contextartifactId>
    4. <version>5.3.30version>
    5. dependency>
  • 相关阅读:
    软件架构模式之第三章:分层架构
    冰蝎Java WebShell免杀生成 -- ByPassBehinder4J​
    小程序中Java后台调用getAccessToken接口、msg_sec_check接口检测文本安全、小程序前端访问后端接口的方法
    开源大数据集群部署(十五)Zookeeper集群部署
    NXP官方Linux内核启动测试
    D22-读论文D22&算法D22
    02 Redis的基本数据结构和常用命令
    Redis的Pipeline简介
    NVMe SSD 学习总结:02 浅析SSD技术基础(掉电保护、U.2 双端口、多命名空间)
    微服务项目:尚融宝(33)(服务调用(3))
  • 原文地址:https://blog.csdn.net/m0_61213696/article/details/136736392