学习如何编写Spring Field Injection示例字段注入Spring框架 依赖注入的一种 。 在本教程中,我们将编写几个类,并看一看现场注入工程。

有关Spring依赖注入的更多信息:

  • Spring Setter依赖注入示例
  • Spring构造函数依赖注入示例
  • Spring依赖注入–字段vs设置器vs构造函数注入
  • Spring依赖注入和控制反转

基于字段的依赖注入

在这种类型的依赖关系注入中,Spring将依赖关系直接分配给字段。 它不同于构造函数注入或基于Setter的依赖注入 。

有趣的是,即使字段是私有的,Spring也会注入依赖项。 Spring使用Java Reflections来做到这一点。 因此,许多专家称它为不安全。

字段注入可能是依赖注入的最简单(但有风险)形式。 为了更好地理解它,我们假设我们有一个正在运行的Spring Boot Service 。 它是一项虚拟服务,旨在了解现场注入。

不知道如何编写Spring Boot Rest Service?
读这个: Spring靴休息服务

想更多地了解Spring Framework?
读这个:

  • Spring框架介绍
  • Spring建筑
  • 春天@Autowired
  • Spring中的@Autowired注释

首先让我们编写DogsService类

狗服务

此类对DogsDao具有依赖关系。 用@Autowired注释参考变量。 有一个setter和两个构造函数 ,分别带有打印消息。

 import com.amitph.spring.dogs.dao.DogsDao;  import com.amitph.spring.dogs.repo.Dog;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.stereotype.Component;  import java.util.List;  @Component  public class DogsService { @Autowired private DogsDao dao; public List<Dog> getDogs() { System.out.println( "DogsService.getDogs called" ); return dao.getAllDogs(); } public void setDao(DogsDao dao) { System.out.println( "DogsService setter called" ); this .dao = dao; } public DogsService(){ System.out.println( "DogsService no-arg constructor called" ); } public DogsService(DogsDao dao) { System.out.println( "DogsService arg constructor called" ); this .dao = dao; }  } 

狗的控制器

控制器对DogsService具有依赖性。 与服务类类似,将@Autowired批注添加到参考变量。 有带有打印消息的setter和构造函数。

 import com.amitph.spring.dogs.repo.Dog;  import com.amitph.spring.dogs.service.DogsService;  import org.springframework.beans.factory.annotation.Autowired;  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 ( "/dogs" )  public class DogsController { @Autowired private DogsService service; @GetMapping public List<Dog> getDogs() { return service.getDogs(); } public void setService(DogsService service) { System.out.println( "DogsController setter called" ); this .service = service; } public DogsController(){ System.out.println( "DogsController no-arg constructor called" ); } public DogsController(DogsService service) { System.out.println( "DogsController arg constructor called" ); this .service = service; }  } 

运行代码

 .  ____         _           __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \  ( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::       (v2. 1.0 .RELEASE)  2019 - 02 - 05 06 : 34 : 14.956 INFO 69421 --- [          main] com.amitph.spring.dogs.Application      : Starting Application on Amits-office-mac.local with PID 69421 (/Users/aphaltankar/Workspace/personal/dog-service-jpa/out/production/classes started by aphaltankar in /Users/aphaltankar/Workspace/personal/dog-service-jpa)  2019 - 02 - 05 06 : 34 : 14.957 INFO 69421 --- [          main] com.amitph.spring.dogs.Application      : No active profile set, falling back to default profiles: default  2019 - 02 - 05 06 : 34 : 15.655 INFO 69421 --- [          main] .sdrcRepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.  2019 - 02 - 05 06 : 34 : 15.711 INFO 69421 --- [          main] .sdrcRepositoryConfigurationDelegate : Finished Spring Data repository scanning in 50ms. Found --- [          main] .sdrcRepositoryConfigurationDelegate : Finished Spring Data repository scanning in 50ms. Found 1 repository interfaces.  2019 - 02 - 05 06 : 34 : 16.013 INFO 69421 --- [          main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$EnhancerBySpringCGLIB$1cc57cd7] is not eligible for getting processed by all BeanPostProcessors ( for example: not eligible for auto-proxying)  2019 - 02 - 05 06 : 34 : 16.318 INFO 69421 --- [          main] osbwembedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)  2019 - 02 - 05 06 : 34 : 16.335 INFO 69421 --- [          main] o.apache.catalina.core.StandardService  : Starting service [Tomcat]  2019 - 02 - 05 06 : 34 : 16.335 INFO 69421 --- [          main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/ 9.0 . 12  2019 - 02 - 05 06 : 34 : 16.342 INFO 69421 --- [          main] oacatalina.core.AprLifecycleListener  : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/aphaltankar/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]  2019 - 02 - 05 06 : 34 : 16.429 INFO 69421 --- [          main] oaccC[Tomcat].[localhost].[/]      : Initializing Spring embedded WebApplicationContext  2019 - 02 - 05 06 : 34 : 16.429 INFO 69421 --- [          main] osweb.context.ContextLoader           : Root WebApplicationContext: initialization completed in 1419 ms  2019 - 02 - 05 06 : 34 : 16.454 INFO 69421 --- [          main] osbwservlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]  2019 - 02 - 05 06 : 34 : 16.457 INFO 69421 --- [          main] osbwservlet.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]  2019 - 02 - 05 06 : 34 : 16.458 INFO 69421 --- [          main] osbwservlet.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]  2019 - 02 - 05 06 : 34 : 16.458 INFO 69421 --- [          main] osbwservlet.FilterRegistrationBean  : Mapping filter: 'formContentFilter' to: [/*]  2019 - 02 - 05 06 : 34 : 16.458 INFO 69421 --- [          main] osbwservlet.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]  2019 - 02 - 05 06 : 34 : 16.581 INFO 69421 --- [          main] com.zaxxer.hikari.HikariDataSource      : HikariPool- 1 - Starting...  2019 - 02 - 05 06 : 34 : 16.702 INFO 69421 --- [          main] com.zaxxer.hikari.HikariDataSource      : HikariPool- 1 - Start completed.  2019 - 02 - 05 06 : 34 : 16.830 INFO 69421 --- [          main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...]  2019 - 02 - 05 06 : 34 : 16.906 INFO 69421 --- [          main] org.hibernate.Version                   : HHH000412: Hibernate Core { 5.3 . 7 .Final}  2019 - 02 - 05 06 : 34 : 16.907 INFO 69421 --- [          main] org.hibernate.cfg.Environment           : HHH000206: hibernate.properties not found  2019 - 02 - 05 06 : 34 : 17.059 INFO 69421 --- [          main] o.hibernate.annotations.common.Version  : HCANN000001: Hibernate Commons Annotations { 5.0 . 4 .Final}  2019 - 02 - 05 06 : 34 : 17.188 INFO 69421 --- [          main] org.hibernate.dialect.Dialect           : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect  2019 - 02 - 05 06 : 34 : 17.783 INFO 69421 --- [          main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'  DogsDao no-arg constructor called  DogsService no-arg constructor called  DogsController no-arg constructor called  2019 - 02 - 05 06 : 34 : 18.208 INFO 69421 --- [          main] ossconcurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'  2019 - 02 - 05 06 : 34 : 18.244 WARN 69421 --- [          main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning  2019 - 02 - 05 06 : 34 : 18.420 INFO 69421 --- [          main] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''  2019 - 02 - 05 06 : 34 : 18.422 INFO 69421 --- [          main] com.amitph.spring.dogs.Application      : Started Application in 3.878 seconds (JVM running for 4.57 ) 

所有三个无参数的控制器按顺序调用。 没有调用setter参数化的构造函数 。 需要注意的另一件事是这些字段,这些字段被标记为@Autowiredprivate

春天可以设置

Spring使用反射在对象上设置私有字段。 这听起来很有用,但另一方面,它也不安全。 现场注射,其安全性和实用性一直备受争议。 Spring不遵守对象访问规则。 现在,有些人可能会支持这种说法,或者说spring IoC容器管理所有对象,应该获得对这些对象的最高控制权。

 @Component  public class MyClass { @Autowired private DogsController controller; @Autowired private DogsService service; @Autowired private DogsDao dao; @Autowired private ApplicationProperties properties;  / business methods  } 

作为开发人员,我一直喜欢使用Field Injection,因为它真的很简单并且可读性更高。 您实际上可以避免编写setter方法构造函数,而将精力放在业务方法上。

摘要

您了解了Field InjectionSpring Dependency Injection的一种 ,并编写了几类来查看它的工作原理。 字段注入很简单,使类更具可读性。 但是,出于安全考虑,许多人讨厌它并避免使用它。

在接下来的部分中,我们将看到Setter注入Field注入Constructor注入 这三种方法之间的详细区别 。

快乐编码!

翻译自: https://www.javacodegeeks.com/2019/02/spring-field-dependency-injection-example.html

Spring字段依赖注入示例相关推荐

  1. Spring Setter依赖注入示例

    学习如何编写Spring Setter依赖注入示例 . Setter注入是Spring依赖注入的一种 . Spring支持字段注入,Setter注入以及构造函数注入,以将依赖项注入Spring托管的b ...

  2. spring注入私有字段_Spring字段依赖注入示例

    spring注入私有字段 了解如何编写Spring Field Injection示例 . 字段注入是Spring框架 依赖注入的一种 . 在本教程中,我们将编写几个类,并看一看现场注入工程. 有关S ...

  3. Spring构造函数依赖注入示例

    欢迎使用Spring构造函数依赖注入示例指南. 基于构造器的依赖注入是Spring 依赖注入的一种 . 依赖注入的另一种类型是Setter注入和字段注入. 有关Spring依赖注入的更多信息: Spr ...

  4. 据说,80%的人没有真正理解了Spring的依赖注入

    前言 提起Spring,大家肯定不陌生,它是每一个Java开发者绕不过去的坎.Spring 框架为基于 java 的企业应用程序提供了一整套解决方案,方便开发人员在框架基础快速进行业务开发. 在官网中 ...

  5. 万字博客带你全面剖析Spring的依赖注入

    1.写在前面 前面的博客我们已经写了Spring的依赖查找,这篇博客我们来了解写Spring的依赖注入. 2.依赖注入的模式和类型 手动模式 - 配置或者编程的方式, 提前安排注入规则 XML 资源配 ...

  6. Spring IoC依赖注入-6

    1. 依赖注入的模式和模型: Spring 提供了哪些依赖注入的模式和类型? 手动模式 - 配置或者编程的方式,提前安排注入规则 XML资源配置元信息 Java 注解配置元信息 API 配置元信息 自 ...

  7. 在ABAP里模拟实现Java Spring的依赖注入

    Dependency Injection- 依赖注入,在Java Spring框架中有着广泛地应用.通过依赖注入,我们不必在应用代码里繁琐地初始化依赖的资源,非常方便. 那么ABAP能否从语言层面上也 ...

  8. 【Spring】依赖注入的几种方式

    在上篇文章中我着重介绍了Spring的控制反转和依赖注入的概念,那么依赖注入有那几种方式呢?他们的优缺点分别是什么,我将在本章中详细讲解. Spring的依赖注入根据对象类型注入可以分为属性注入和对象 ...

  9. java中四种注入注解,Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

最新文章

  1. 微信小程序 python接口_微信小程序-封装请求基准路径、接口API 和使用
  2. 从0到1,网上搜不到的企业信息安全搭建全过程,这本书讲透了!
  3. terminal怎么运行c语言文件,在mac电脑的terminal里该如何运行c语言
  4. vue打包关闭console.log
  5. 性能测试—接口压测指标分析
  6. PAT1044 火星数字 (20 分)
  7. C++设计模式详解之命令模式解析
  8. java2的7次方怎么表示_一元二次方程常见题型之方程根的解法
  9. 百万数据php7取出循环_Thinkphp5 分批批量导出百万条数据记录的Code,不用PHPEXCEL哦!...
  10. systemd(CentOS7)启动zookeeper
  11. [原创]超轻量级Web安全漏洞扫描工具Netsparker使用教程介绍
  12. FastAPI 教程翻译 - 介绍
  13. IDEA导入一个项目
  14. 嵌入式linux操作framebuffer显示bmp图片
  15. [Qt笔记]设置VS2015下的应用工程图标
  16. Colorbox 参数设置-中文版
  17. 大家保险发布“早下班一小时”倡议,呼吁“重阳节,不脱节”
  18. 2023年国家留学基金委(CSC)有关国别申请、派出注意事项
  19. 十六进制颜色转换ARGB/RGB
  20. originPro2021(3)添加图例导出图片图例不完全

热门文章

  1. P3327 约数的个数和 [约数函数性质,数论分块]
  2. 费用流-Wannafly Day2 TwoGraph-神题
  3. 7、oracle下的序列
  4. Java并发编程包中atomic的实现原理
  5. Oracle入门(四)之查询基本信息
  6. 【git】如何给github绑定ssh
  7. C++描述杭电OJ 2018.母牛的故事 ||
  8. 《走遍中国》珍藏版(一)
  9. Flume整合SparkStreaming出现UnsupportedClassVersionError: org/apache/spark/streaming/flume/sink/SparkFlum
  10. MybatisPlus学习(四)条件构造器Wrapper方法详解