injection(注入)

injection(注入)的意思:

    Java EE提供了注入机制,使您的对象能够获取对资源和其他依赖项的引用,而无需直接实例化它们。通过使用将字段标记为注入点的注释之一来装饰字段或方法,可以在类中声明所需的资源和其他依赖项。然后容器在运行时提供所需的实例。注入简化了代码并将其与依赖项的实现分离。

资源注入

  资源注入使您能够将JNDI名称空间中可用的任何资源注入到任何容器管理的对象中,例如servlet、企业bean或托管bean。例如,可以使用资源注入来注入JNDI名称空间中可用的数据源、连接器或自定义资源。

依赖注入

  依赖注入使您可以将常规Java类转换为托管对象并将其注入到任何其他托管对象中。使用依赖注入,代码可以声明对任何托管对象的依赖。容器在运行时在注入点自动提供这些依赖项的实例,它还为您管理这些实例的生命周期。JavaEE中的依赖注入定义了作用域,它决定了容器实例化和注入的对象的生命周期。例如,只需要响应单个客户机请求(如货币转换器)的托管对象与在会话(如购物车)中处理多个客户机请求所需的托管对象具有不同的范围。

一些使用Java依赖注入的好处如下:

    关注点分离

    应用程序类中的样板代码减少,因为所有用于初始化依赖性的工作都由注入器组件处理

    配置组件使应用程序易扩展 通过模拟对象来单元测试会很简单

Java依赖注入的缺点

    如果过度使用,可能会导致维护问题,因为更改的影响只有在运行时才知道。

    Java中的依赖注入可能会隐藏导致运行时错误的服务类的依赖性,这会在编译时被捕获。 以上就是Java中的依赖注入模式。当我们控制服务时,了解和使用它是很好的

转载于:https://www.cnblogs.com/TobaccoPLK/p/10834841.html

Editorial Reviews Product Description Dependency Injection is an in-depth guide to the current best practices for using the Dependency Injection pattern-the key concept in Spring and the rapidly-growing Google Guice. It explores Dependency Injection, sometimes called Inversion of Control, in fine detail with numerous practical examples. Developers will learn to apply important techniques, focusing on their strengths and limitations, with a particular emphasis on pitfalls, corner-cases, and best practices. This book is written for developers and architects who want to understand Dependency Injection and successfully leverage popular DI technologies such as Spring, Google Guice, PicoContainer, and many others. The book explores many small examples of anchor concepts and unfolds a larger example to show the big picture. Written primarily from a Java point-of-view, this book is appropriate for any developer with a working knowledge of object-oriented programming in Java, Ruby, or C#. About the Author Dhanji R. Prasanna is an Enterprise Java consultant for technologies such as EJB3, JBI, JSF, Guice, Spring, HiveMind, and PicoContainer. He is a co-author of the Bean Validation (JSR-303), JAX-RS (JSR-311), Servlet 3.0 (JSR-315), and JavaServerFaces 2.0 (JSR-314) specifications. He is also co-author of the Java EE 6.0 (JSR-316) platform specification, which is the next edition of J2EE. Product Details * Paperback: 352 pages * Publisher: Manning Publications; 1 edition (August 28, 2009) * Language: English * ISBN-10: 193398855X * ISBN-13: 978-1933988559 * Product Dimensions: 9.1 x 7.4 x 0.8 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值