• @Autowired注解推荐使用方法:用在构造方法上


    说明

    @Autowired注解,用于自动将一个对象注入到当前的对象中。

    spring 推荐使用构造器注入的方式。

    spring 不推荐@Autowired注解用于字段

    构造器注入

    依赖注入,通过@Autowired注解,用在构造方法上。

    如果只有一个构造方法,则@Autowired注解可以省略

    示例

    在这里插入图片描述

    代码

    package com.example.web.controller;
    
    import com.example.web.entity.User;
    import com.example.web.service.UserService;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("users")
    public class UserController {
    
        private final UserService userService;
    
    
        public UserController(UserService userService) {
            this.userService = userService;
        }
    
    
        @GetMapping
        public List<User> selectAll() {
            return userService.list();
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    经过测试,如上的代码中,userMapper 对象,可以被正确注入到Controller对象中。

    单元测试,必须添加@Autowired注解

    在单元测试时,通过构造器依赖注入,未添加@Autowired注解,运行单元测试方法时会报错。

    单元测试时,构造器上必须添加@Autowired注解。

    报错信息

    org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [com.example.web.service.UserService userService] in constructor [com.example.ServiceTest(com.example.web.service.UserService)].

    正确代码(添加了@Autowired)

    package com.example;
    
    import com.example.web.entity.User;
    import com.example.web.service.UserService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    class ServiceTest {
    
        private final UserService userService;
    
    
        @Autowired
        ServiceTest(UserService userService) {
            this.userService = userService;
        }
    
    
        @Test
        void list() {
            List<User> list = userService.list();
            System.out.println(list);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    Unity关于无法新建项目的可能解决办法
    支持向量机(一)
    系统配置与性能评价
    【Git】idea提交项目到Gitee
    JAVA 0基础 数据类型 整数型
    PowerCLI 通过vCenter 批量导出所有虚拟机到本地磁盘
    教育行业如何通过互联网推广品牌?媒介盒子告诉你
    【数据结构】广义表(列表)
    [数据集][目标检测]老鼠检测数据集VOC+YOLO格式4107张1类别
    代码随想录算法训练营第16天|104. 二叉树的最大深度111.二叉树的最小深度222.完全二叉树的节点个数
  • 原文地址:https://blog.csdn.net/sgx1825192/article/details/133083633