- why
Java EE provides injection mechanisms that enable your objects to obtain references to resources and other dependencies without having to instantiate them directly. You declare the required resources and other dependencies in your classes by decorating fields or methods with one of the annotations that mark the field as an injection point. The container then provides the required instances at runtime. Injection simplifies your code and decouples it from the implementations of its dependencies.
Java EE提供了注入机制,使您的对象能够获取对资源和其他依赖项的引用,而无需直接实例化它们。 通过使用将字段标记为注入点的注释之一来装饰字段或方法,可以在类中声明所需的资源和其他依赖项。 然后容器在运行时提供所需的实例。 注入简化了代码并将其与依赖项的实现分离。
- classfication
-
Resource injection enables you to inject any resource available in the JNDI namespace into any container-managed object, such as a servlet, an enterprise bean, or a managed bean. For example, you can use resource injection to inject data sources, connectors, or custom resources available in the JNDI namespace.
The type you use for the reference to the injected instance is usually an interface, which decouples your code from the implementation of the resource.
通过资源注入,您可以将JNDI名称空间中可用的任何资源注入任何容器管理的对象,例如servlet,企业bean或托管bean。 例如,您可以使用资源注入来注入JNDI名称空间中可用的数据源,连接器或自定义资源。 用于引用注入实例的类型通常是一个接口,它将您的代码与资源的实现分离。
-
Dependency injection enables you to turn regular Java classes into managed objects and to inject them into any other managed object. Using dependency injection, your code can declare dependencies on any managed object. The container automatically provides instances of these dependencies at the injection points at runtime, and it also manages the lifecycle of these instances for you.
Dependency injection in Java EE defines scopes, which determine the lifecycle of the objects that the container instantiates and injects. For example, a managed object that is only needed to respond to a single client request (such as a currency converter) has a different scope than a managed object that is needed to process multiple client requests within a session (such as a shopping cart).
依赖注入使您可以将常规Java类转换为托管对象,并将它们注入任何其他托管对象。 使用依赖注入,您的代码可以声明对任何托管对象的依赖性。 容器在运行时自动在注入点提供这些依赖项的实例,并且还为您管理这些实例的生命周期。 Java EE中的依赖注入定义了范围,这些范围确定容器实例化和注入的对象的生命周期。 例如,仅响应单个客户端请求(例如货币转换器)的托管对象与在会话中处理多个客户端请求(例如购物车)所需的托管对象具有不同的范围。
- history
- trend
- future