CDI Framework Notes-weld reference Chinese translation (2)

If you need to reprint, please indicate the source, thank you http://equalxx.iteye.com/

This time the code formatting is a bit messy

 

Chapter 1.Introduction

Are you desperate to start writing your first bean right now? Or you're hesitant, not knowing where the CDI specification is going to be hard. Don't worry, you've probably written hundreds of beans already, CDI just makes it easier for you to create an application with them!

 

1.1   what is a bean

A bean is what you think it is. Also now that it is defined in the container environment, before java EE 6 , the term "bean" was not clearly defined on the javaEE platform . Of course, we've been calling Java classes in web and enterprise applications beans for many years. There are even several different things called beans in the javaEE specification, including EJB beans and JSF managed beans . Meanwhile, other third-party frameworks, such as Spring and Seam , introduced their own understanding of beans . We lack an official public definition.

 

Finally Java EE 6 gives the official definition of beans in the Managed Bean specification . Beans are defined as container-managed objects with minimal programming restrictions, and in some cases are called POJOs ( Plain Old Java Objects ). They provide a few basic services like resource injection, lifecycle callbacks, and interceptors. These specifications, such as EJB and CDI , are built on this foundation. Ultimately, however, these bean and lightweight component models will be unified and consistent across JavaEE platforms.

 

Most of the time (with few exceptions), a concrete class whose constructor has no parameters (or a constructor is annotated with @Inject ) is treated as a bean . This includes all JavaBeans and all EJB Session Beans ( EJB Session Beans ). If you have already written JavaBean and EJB beans , you have already touched beans , and you don't need to add any special configuration on top of this. The beans you write every day have so far not used the new services in the CDI specification, but you can now try to use CDI to operate on any bean : allowing the container to create and destroy instances of the bean and associate them with the specified Context-affinity, injecting them into other beans , using them in EL expressions, annotating them with qualifying annotations, and even adding interceptors and decorators to them - it doesn't require you to modify existing code, at most Just add a note. Now let's create your first CDI -generated bean .

 

1.2   Getting our feet wet 试水

 

Suppose we have two existing Java classes that we have been using for many years in various applications. The first class converts a string into a list of sentences :

        

public class SentenceParser {
      public List<String> parse(String text) { ... }
}

 
 

 

The second class is a stateless session bean that is invoked by the front end of the external system to translate sentences from one language to another:

 

@Stateless
public class SentenceTranslator implements Translator {
      public String translate(String sentence) { ... }
}

 

 

where Translator is the EJB native interface:

 

@Local
public interface Translator {
public String translate(String sentence);
}

 

 

Let's write a bean that translates the entire text:

public class TextTranslator {
private SentenceParser sentenceParser;
private Translator sentenceTranslator;
@Inject
TextTranslator(SentenceParser sentenceParser, Translator sentenceTranslator) {
this.sentenceParser = sentenceParser;
this.sentenceTranslator = sentenceTranslator;
}
public String translate(String text) {
StringBuilder sb = new StringBuilder();
for (String sentence: sentenceParser.parse(text)) {
sb.append(sentenceTranslator.translate(sentence));
}
return sb.toString();
}
}

 

 

但等等,没有无参数构造器的TextTranslator是否仍然是个bean?如果你还记得的话,一个类没有无参构造器,但如果有@Inject注解的话,它仍然是个bean

 

正如你所想,@Inject和依赖注入有关!@Inject适用于bean的构造器或者方法,然后容器在初始化的时候调用这些构造器或者方法。容器也可以通过这些构造器和方法的参数来注入其他bean

 

我们可以获得TextTranslator的实例通过将其注入到一个bean构造函数、方法、域中,或是一个JavaEE组件类的域或方法(如servlet)。容器选择被注入的对象是通过注入地方的类型,而不是域、方法或者参数的名称。

 

让我们来创建一个通过域注入来获得TextTranslator实例的UI Controller bean吧,用来翻译用户输入的文本。

 

@Named @RequestScoped
public class TranslateController {
@Inject TextTranslator textTranslator;
private String inputText;
private String translation;
// JSF action method, perhaps
public void translate() {
translation = textTranslator.translate(inputText);
}
public String getInputText() {
return inputText;
}
public void setInputText(String text) {
this.inputText = text;
}
public String getTranslation() {
return translation;
}
}

 

 

小贴士:

注意这个Controller是被标注了request-scopednamed的。因为现在web应用里同时用这俩注释非常常见,所以CDI里有个内置的注解@Model,如果用了@Model,就相当于创建了一个@request-scope,@Named bean

 

另外,我们也可以通过编程方式用bean的类型作为参数,从一个被注入的实例的实例获取一个TextTranslator实例,语言说起来有点鸡肋,看代码吧:

import javax.enterprise.inject.Instance;
import javax.inject.Inject;
....
@Inject Instance<TextTranslator> textTranslatorInstance;
...
public void translate() {
textTranslatorInstance.get().translate(inputText);
}

 

 

请注意没必要为了把一个bean注入到另一个bean而去写gettersetter方法。CDI可以直接访问一个被注入的域(甚至它是私有的),这样可以少些无用代码。域的名字可以任意命名,它决定了什么会被注入。

 

在系统初始化期间,容器必须验证注入的类的存在。在我们的例子中,如果没有Translator的实现方法(如果SentenceTranslator EJB没有加载),容器会通知我们这里有个问题依赖(unsatisfied

dependency)。如果有多个Translator的实现类,容器会通知我们依赖模糊(ambiguous dependency)。

 

在我们深入细节以前,让我们先停下来检查一下bean的结构。bean的哪方面最重要,是什么让它最重要。所以与其给你看bean的例子,我们将要找出是什么组成了一个bean

 

如需转载,请注明出处,谢谢http://equalxx.iteye.com/

 

 

Guess you like

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