CDI Framework Notes-weld reference Chinese translation (1)-CDI Reference Implementation

Our next work goal is to use CDI to achieve. The architect provided our learning document, he said that there is no good Chinese material other than this document

Weld 2.4.1.Final - CDI Reference Implementation

Document address: http://docs.jboss.org/weld/reference/latest/en-US/html_single/

PDF download address: http://docs.jboss.org/weld/reference/latest/en-US/pdf/weld-reference.pdf

 

Since I like to think carefully when reading books (this is really not a good habit, it takes more than half a year to read a book, which is a pain in the ass), so since I have to take notes, I simply translate it. The document itself is also very concise, no nonsense, so it can be used as a note after translation.

 

Please respect the results, if you need to reprint, please indicate the source, thank you for your cooperation:) http://equalxx.iteye.com/, the number of pages of the document is acceptable, and it will be updated every day in the near future. I am also a novice, so I hope everyone can point out and correct any problems in translation. Most programmers can read in English, so it is also recommended to read English as much as possible to understand some concepts more accurately.

 

Special thanks to Google Translate.

 

 

 

Part I. Beans

 

CDI services provide:

  1. For state objects, bind their improved lifecycle in a well-defined context
  2. A safe dependency injection method,
  3. Interaction between objects through event message mechanism
  4. A better way to attach interceptors to objects, and a new type of interceptor called a decorator, which is more suitable for solving business problems.
  5. SPI for developing portable extensions for containers

 

Objects bound to a lifecycle context are called beans . CDI includes built-in support for several

不同种类的bean,包括以下Java EE组件类型:

  1. 托管bean
  2. EJB 会话bean

 

托管beanEJB会话bean都可能注入其他bean 但是一些其他的对象,

其本身不是本文使用的意义上的bean,也有可以通过CDI注入的bean

Java EE平台中,以下类型的组件可能注入了bean

  1. 消息驱动的bean
  2. 拦截器,
  3. servletservlet筛选器和servlet事件监听器,
  4. JAX-WS服务端点和处理程序,
  5. JAX-RS资源,提供程序和javax.ws.rs.core.Application子类
  6. JSP标记处理程序和标记库事件监听器。

 

CDI解决了一些不熟悉API用户的以下疑问:

  1. 这个对象的生命周期是什么
  2. 它能同时有多少客户端
  3. 它是多线程的吗
  4. 我如何从客户端访问它
  5. 我需要显式地销毁它吗
  6. 在我当前没有使用它的时候,我需要在哪里保持引用,
  7. 我该怎样定义一个可替换的实现方法,以方便不同的部署场景
  8. 我该如何跟其它对象共享此对象

 

CDI不仅仅是一个框架,它是一个完整的编程框架。CDI的主旨是强类型松耦合。让我们研究下这句话是什么意思。

 

一个bean只规定了它所依赖的其它bean的类型和语义,它不需要知道确切的生命周期,具体实现,线程模型,或者任何和bean互动的其他客户端。但更强大的是,一个bean的具体实现,生命周期和线程模型可以根据部署场景而变化,而不影响任何客户端。这种松耦合可以使代码更容易维护。

 

事件,拦截器和装饰器增强了这种模型中固有的松耦合:

  1. 用事件消息机制分离事件产生者和消费者
  2. 用拦截器分离技术实现和业务逻辑
  3. 装饰器允许划分业务关系

 

更强的是,CDI在提供这些性能的同时保持着类型安全。CDI管理对象并不依赖基于字符串的标识符,取而代之的是,CDI利用的是java对象模型中已经写入的信息,使用一种新的编程模式(叫做限定符注解),去管理bean,及它们的依赖,以及拦截器,装饰器,和它们的事件消费者。部署信息使用最精简的XML描述符来设定。

 

但是CDI不是一个局限的编程模型,它不会指定你怎么去构建你的应用程序,怎么处理持久性,或是规定用什么web框架。这些东西都是由你自己决定的。

 

 

CDI提供了一个全面SPI,可以让未来javaEE规范定义的其他类型的对象,或是第三方的框架,利用CDI服务和对其他类型对象的交互来进行无缝整合。CDI受到许多现有Java框架的影响,包括SeamGuiceSpring。但CDI有着自己独特的特性,比Seam更安全(类型安全),比Spring更具状态性且不那么多xml配置,比Guice有更多的web和企业级应用。但是它离不开来自这些框架的启发以及辛勤工作的JSR-299JSR-346专家组(EG)。最后,CDI是一个Java Community Process [http://jcp.org]JCP)标准。 Java EE 7要求所有兼容的应用程序服务器都要支持JSR-346(甚至在Web配置文件中)。

 

未完待续

请尊重成果,如需转载请注明出处,谢谢合作:)http://equalxx.iteye.com/

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326840697&siteId=291194637