spring注入私有字段

了解如何编写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?

读这个: Sprint Boot休息服务

想更多地了解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参数化的构造函数 。 还有一点要注意的是标记为@Autowired的字段是private

春天可以设置

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方法构造函数,而将精力放在业务方法上。

摘要

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

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

快乐编码!

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

spring注入私有字段

spring注入私有字段_Spring字段依赖注入示例相关推荐

  1. 【转载】详解 Spring 3.0 基于 Annotation 的依赖注入实现

    转载自:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-iocannt/ Spring 的依赖配置方式与 Spring 框架的 ...

  2. 详解 Spring 3.0 基于 Annotation 的依赖注入实现--转载

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

  3. Spring学习-理解IOC和依赖注入

    最近刚买了一本介绍ssm框架的书,里面主要对Mybatis.spring.springmvc和redis做了很多的讲解,个人觉得虽然有的内容我看不懂,但是整体上还是不错的.最近正在学习中,一边学习一边 ...

  4. 使用Spring.Net对Web页面进行依赖注入

    今天看到这篇文章 Unity&WebForm(1): 自定义IHttpHandlerFactory使用Unity对ASP.NET Webform页面进行依赖注入,这是一个很好的思路,自定义IH ...

  5. Spring容器,控制反转,依赖注入

    Spring boot学习之旅,为更好督促自己学习以记之,仅供参考. spring容器 程序启动的时候会创建spring容器,扫描给spring容器一个清单,比如:@Controller, @Bean ...

  6. 2.4 Spring Framework 5.x之DI(依赖注入)

    依赖注入(DI) 依赖注入(DI)是一个过程,通过这个过程,对象只能通过构造函数参数,工厂方法的参数或在构造对象实例后在对象实例上设置的属性来定义它们的依赖关系(即,它们使用的其他对象).从工厂方法返 ...

  7. java-12:spring MVC - 控制反转IOC,依赖注入DI

    学习spring框架之前,先理解几个概念: 1.第一部分:依赖倒置原则 2.第二部分:控制反转,控制反转容器(实例) 3.第三部分:控制反转,控制反转容器(全面理解,面试题) 综合性理解:控制反转(I ...

  8. Spring Ioc/DI 控制反转和依赖注入

    Ioc(Inversion of Control)和DI(Dependency Injection)的全称分别是控制反转和依赖注入. 如何理解这两个概念呢? Ioc Ioc(控制反转)实际上就说把对象 ...

  9. spring中的控制反转和依赖注入之间的关系

    Spring中的控制反转:把new这一个过程交给了spring容器去处理. 控制反转就是将new对象这一个过程交给外部去做(即Spring)而不是自己去创建. 图中的1"控制正转" ...

最新文章

  1. 飞桨模型保存_重磅发布开源框架、生物计算平台螺旋桨,百度飞桨交了年终成绩单...
  2. 撩课-Web大前端每天5道面试题-Day38
  3. java课程 数独 文库_数独java
  4. 【大数据-Hadoop】Hadoop架构
  5. 【Python】青少年蓝桥杯_每日一题_1.19_Python 求和
  6. 一ElasticSearch安装启动
  7. 显示器、显卡的接口类型
  8. 水系图一般在哪里找得到_水系电池再发Nature,事实力证或将迎来发展的春天!...
  9. 开始编写寄几的 CSS 基础库
  10. mysql 月初 月末_月初月末sql语句(日期所在月的第一天,最后一天)
  11. [构造]triples I
  12. Win10+yolov5 踩坑记录
  13. 如何使用Keil5中的虚拟示波器进行软件仿真
  14. 关于Axure的基本框架
  15. 利用appimage工具对开发好的项目进行打包
  16. 大数据系列 -- 数据埋点
  17. Prolog教程 3
  18. 声源定位matlab模拟,matlab声源定位程序的一点小问题
  19. 【松岩论道】一个道理说明股市人的发展不是靠江湖式的随机跳跃分析!
  20. UVM实战 卷I学习笔记10——UVM中的寄存器模型(3)

热门文章

  1. 牛客题霸 [买卖股票的最好时机] C++题解/答案
  2. P4548-[CTSC2006]歌唱王国【概率生成函数,KMP】
  3. 牛客-十二桥问题【最短路,状压dp】
  4. CF613D-Kingdom and its Cities【虚树,LCA,树链剖分,贪心】
  5. jzoj1764-游戏【dp,dfs】
  6. POJ1456-Supermarket【并查集】
  7. codeforces1552 D. Array Differentiation(思维+暴力)
  8. 2019.01.23【NOIP普及组】模拟赛C组总结
  9. UVA10601 Cubes - 波利亚定理
  10. 面试阿里挂了却拿到网易、点我达offer,一个三年经验Java程序员的面试总结