springboot依赖注入的几种方式

简介: springboot依赖注入的几种方式

Spring Boot 提供了多种方式来实现依赖注入:

  1. 构造方法注入:通过在类的构造方法上使用@Autowired注解,将依赖通过构造方法的参数进行注入。

javaCopy Code

@ComponentpublicclassExampleService {

    private DependencyService dependencyService;

    @Autowired
    publicExampleService(DependencyService dependencyService) {
        this.dependencyService = dependencyService;
    }
}
  1. Setter 方法注入:通过在类的Setter方法上使用@Autowired注解,将依赖通过Setter方法进行注入。

javaCopy Code

@ComponentpublicclassExampleService {

    private DependencyService dependencyService;

    @Autowired
    publicvoidsetDependencyService(DependencyService dependencyService) {
        this.dependencyService = dependencyService;
    }
}
  1. 字段注入:通过在类的字段上使用@Autowired注解,将依赖直接注入到字段中。

javaCopy Code

@ComponentpublicclassExampleService {

    @Autowired
    private DependencyService dependencyService;
}
  1. 方法注入:通过在类的方法上使用@Autowired注解,将依赖通过方法参数进行注入。可以用在普通方法、工厂方法和Bean生命周期回调方法上。

javaCopy Code

@ComponentpublicclassExampleService {

    private DependencyService dependencyService;

    @Autowired
    publicvoidsetDependencyService(DependencyService dependencyService) {
        this.dependencyService = dependencyService;
    }

    @Autowired
    publicvoidinit(DependencyService dependencyService) {
        // ...
    }
}
  1. 注解限定符(Qualifier):当存在多个相同类型的依赖时,可以通过@Qualifier注解指定具体的Bean进行注入。

javaCopy Code

@ComponentpublicclassExampleService {

    @Autowired
    @Qualifier("specificDependency")
    private DependencyService dependencyService;
}
  1. 自定义注解:通过创建自定义注解和对应的注解处理器,可以实现更灵活的依赖注入方式。

javaCopy Code

@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)@Autowiredpublic@interface MyCustomAnnotation {
    // ...
}

@ComponentpublicclassExampleService {

    @MyCustomAnnotation
    private DependencyService dependencyService;
}

这些方式都是基于 Spring 的依赖注入机制来实现的,Spring Boot 会根据配置和约定自动完成依赖的查找和注入。开发者可以根据项目的需求选择合适的依赖注入方式。

目录
相关文章
|
2天前
|
Java 容器 Spring
SpringBoot:详解依赖注入和使用配置文件
SpringBoot:详解依赖注入和使用配置文件
|
2天前
|
前端开发 Java API
Spring Boot之Spring MVC基于注解的控制器(RequestMapping注解类型 重定向与转发 依赖注入)
Spring Boot之Spring MVC基于注解的控制器(RequestMapping注解类型 重定向与转发 依赖注入)
48 0
|
2天前
|
Java 容器 Spring
【SpringBoot:详解依赖注入和使用配置文件】
【SpringBoot:详解依赖注入和使用配置文件】
14 2
|
SQL XML Java
简单理解springboot的依赖注入
依赖注入,Dependency Injection,简称DI,是spring中的核心技术,此技术贯穿Spring全局,是必须要熟练掌握的知识点。在本文中,我们将要深入研究spring中的IOC和DI,理解核心思想,并学会如何在spring boot中使用基于java和注解的方式正确使用DI来创建spring应用程序。控制反转 IOC要理解DI,首先需要理解spring的核心思想之一,控制反转(In
1525 0
简单理解springboot的依赖注入
|
12月前
|
XML Java 程序员
Spring Boot2.x-05Spring Boot基础-使用注解完成依赖注入
Spring Boot2.x-05Spring Boot基础-使用注解完成依赖注入
82 0
Spring Boot2.x-05Spring Boot基础-使用注解完成依赖注入
|
安全 Java 程序员
SpringBoot 依赖注入的优雅实现
SpringBoot 依赖注入的优雅实现
477 0
|
2天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
2天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
109 2
|
2天前
|
前端开发 JavaScript Java
6个SpringBoot 项目拿来就可以学习项目经验接私活
6个SpringBoot 项目拿来就可以学习项目经验接私活
42 0
|
2天前
|
Java 应用服务中间件 Maven
Spring Boot项目打war包(idea:多种方式)
Spring Boot项目打war包(idea:多种方式)
13 1