qidao123.com技术社区-IT企服评测·应用市场

标题: Springboot 注入方式 [打印本页]

作者: 西河刘卡车医    时间: 2025-3-6 21:21
标题: Springboot 注入方式
在 Spring Boot 中,依靠注入是一种紧张的设计模式,用于实现组件之间的解耦。常见的注入方式紧张有以下几种:
1. 构造函数注入


  1. import org.springframework.stereotype.Service;
  2. // 定义一个服务接口
  3. interface MyService {
  4.     void doSomething();
  5. }
  6. // 实现服务接口
  7. @Service
  8. class MyServiceImpl implements MyService {
  9.     @Override
  10.     public void doSomething() {
  11.         System.out.println("Doing something...");
  12.     }
  13. }
  14. // 使用构造函数注入依赖
  15. @Service
  16. class MyClient {
  17.     private final MyService myService;
  18.     // 构造函数注入
  19.     public MyClient(MyService myService) {
  20.         this.myService = myService;
  21.     }
  22.     public void performAction() {
  23.         myService.doSomething();
  24.     }
  25. }
复制代码
2. Setter 方法注入


  1. import org.springframework.stereotype.Service;
  2. // 定义一个服务接口
  3. interface MyService {
  4.     void doSomething();
  5. }
  6. // 实现服务接口
  7. @Service
  8. class MyServiceImpl implements MyService {
  9.     @Override
  10.     public void doSomething() {
  11.         System.out.println("Doing something...");
  12.     }
  13. }
  14. // 使用 Setter 方法注入依赖
  15. @Service
  16. class MyClient {
  17.     private MyService myService;
  18.     // Setter 方法注入
  19.     public void setMyService(MyService myService) {
  20.         this.myService = myService;
  21.     }
  22.     public void performAction() {
  23.         if (myService != null) {
  24.             myService.doSomething();
  25.         }
  26.     }
  27. }
复制代码
3. 字段注入


  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. // 定义一个服务接口
  4. interface MyService {
  5.     void doSomething();
  6. }
  7. // 实现服务接口
  8. @Service
  9. class MyServiceImpl implements MyService {
  10.     @Override
  11.     public void doSomething() {
  12.         System.out.println("Doing something...");
  13.     }
  14. }
  15. // 使用字段注入依赖
  16. @Service
  17. class MyClient {
  18.     @Autowired
  19.     private MyService myService;
  20.     public void performAction() {
  21.         myService.doSomething();
  22.     }
  23. }
复制代码
4. 方法注入


  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. // 定义一个服务接口
  4. interface MyService {
  5.     void doSomething();
  6. }
  7. // 实现服务接口
  8. @Service
  9. class MyServiceImpl implements MyService {
  10.     @Override
  11.     public void doSomething() {
  12.         System.out.println("Doing something...");
  13.     }
  14. }
  15. // 使用方法注入依赖
  16. @Service
  17. class MyClient {
  18.     private MyService myService;
  19.     @Autowired
  20.     public void initialize(MyService myService) {
  21.         this.myService = myService;
  22.     }
  23.     public void performAction() {
  24.         myService.doSomething();
  25.     }
  26. }
复制代码
在实际开发中,构造函数注入通常是首选的注入方式,因为它具有更好的可维护性和可测试性。而字段注入虽然代码轻便,但在一些复杂场景下大概会带来一些题目,应谨慎使用。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4