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";
    }

}
相关推荐
超级小忍10 分钟前
服务端向客户端主动推送数据的几种方法(Spring Boot 环境)
java·spring boot·后端
字节跳跃者12 分钟前
为什么Java已经不推荐使用Stack了?
javascript·后端
字节跳跃者12 分钟前
深入剖析HashMap:理解Hash、底层实现与扩容机制
javascript·后端
程序无bug14 分钟前
Spring IoC注解式开发无敌详细(细节丰富)
java·后端
程序无bug17 分钟前
Spring 对于事务上的应用的详细说明
java·后端
食亨技术团队18 分钟前
被忽略的 SAAS 生命线:操作日志有多重要
java·后端
程序员NEO18 分钟前
精控Spring AI日志
人工智能·后端
考虑考虑34 分钟前
Maven 依赖范围(Scope)
java·后端·maven
张小洛41 分钟前
Spring AOP 设计解密:代理对象生成、拦截器链调度与注解适配全流程源码解析
java·后端·spring·spring aop·aop
00后程序员1 小时前
iOS 性能测试工具全流程:主流工具实战对比与适用场景
后端