Spring依赖注入的三种方式

1、 settter方法注入 以@Autowired注解为例,即把@Autowired注解标记在目标bean的引用bean的setter方法上。

kotlin 复制代码
@RestController
@RequestMapping("/abc")
public class AbcController {


    private AbcService abcService;

    @Autowired
    public void setAbcService(AbcService abcService) {
        this.abcService = abcService;
    }

    @GetMapping("/rateLimit")
    public  String  rateLimit(){

        System.out.println("start");
        abcService.printABC();
        return "abc";
    }

}

2.构造器注入

kotlin 复制代码
@RestController
@RequestMapping("/abc")
public class AbcController {


    private AbcService abcService;
    
    public AbcController(AbcService abcService) {
        this.abcService = abcService;
    }

    @GetMapping("/rateLimit")
    public  String  rateLimit(){

        System.out.println("start");
        abcService.printABC();
        return "abc";
    }

}

3.变量(filed) 注入

kotlin 复制代码
@RestController
@RequestMapping("/abc")
public class AbcController {

    @Autowired
    private AbcService abcService;
    

    @GetMapping("/rateLimit")
    public  String  rateLimit(){

        System.out.println("start");
        abcService.printABC();
        return "abc";
    }

}
相关推荐
ywf12151 小时前
前端的dist包放到后端springboot项目下一起打包
前端·spring boot·后端
程序员爱钓鱼1 小时前
Go排序核心库: sort包深度指南
后端·面试·go
大阿明7 小时前
Spring Boot(快速上手)
java·spring boot·后端
墨香幽梦客8 小时前
API集成技术规范:RESTful与GraphQL在企业系统对接中的应用对比
后端·restful·graphql
百锦再8 小时前
Java 并发编程进阶,从线程池、锁、AQS 到并发容器与性能调优全解析
java·开发语言·jvm·spring·kafka·tomcat·maven
刀法如飞9 小时前
AI编程时代,为什么35岁以上程序员会更吃香?
人工智能·后端·ai编程
小码哥_常10 小时前
Spring Boot 遇上 HMAC-SHA256,API 安全大升级!
后端
小码哥_常10 小时前
10分钟极速掌握!SpringBoot+Vue3整合SSE实现实时消息推送
后端
NGC_661111 小时前
SSM vs SpringBoot+MyBatis 对比
spring
大黄说说11 小时前
深入 Go 语言 GMP 调度模型:高并发的秘密武器
后端