CDI (contexts and dependency injection) is
Javaee6.
Dependency InjectionIOC/DI is upgraded to the container level. It provides the Core Component Management for service injection on the Java EE platform. Simplified should be the goal of CDI, so that everything can be annotated and injected.
The contexts concept is different from the business scenario discussed previously in the DCI architecture, including the meaning of the container Technical Architecture scenario. The scenario includes four types: Request (event), session, application, page, the seam framework expands two conversation and business process context.
CDI has made great changes to the use of the JSF and EJB models. For example, the following shows a bean used to display JSF:
import javax.inject.Named;
@Named public class MessageServerBean {
public String getMessage() { return "Hello World!"; } }
|
If @ named is used for annotation, the tag can be written to the JSP page:
Message is : #{messageServerBean.message}<br> Message Server Bean is : #{messageServerBean}
|
The output result is as follows:
Message is : Hello World! Message Server Bean is : [email protected]
|
That is to say, @ named annotation is equivalent to naming messageserverbean eedemo. messageserverbean (you can also explicitly write a name), and you can directly output the getter method using messageserverbean.
Another feature of CDI is to mark the lifecycle of an object in a container scenario as follows:
@ Named ("itemprocessor ") @ Requestscoped// Indicates that the life cycle is a request. Each request ends, and the life ends. You can also have a session or application. Public ClassItemprocessor {
@ Inject PrivateItemdao;// Indicates that itemdao needs to be injected
... }
|
As you can see, these tricks are already widely used in spring or our jdonframework.
Dependency Injection. It is an automatic pairing of auto-wired injection, not the manual configuration dependency in spring 1.x. In my post in, IOC container's revolutionary advantages brought forward the epoch significance of automatic injection. For a while, I had to boast about the difference between jdonframework and spring 1.x for a long time. It is now common.
CDI also provides the producer method, that is, the implementation of the factory method, so that you can customize some of your own things before the object is injected.
public class PersonFactory {
@Produces @RequestScoped public Person createPerson() { return new Person(); } }
|
However, before the person is injected into other places where the person is needed, createperson is executed first. In this method, you can prepare for the injection.
CDI also provides events event injection to enable asynchronous event Mode
Javaee.
Event messages are divided into producers and consumers. For details, see event-listerner event listening mode. The message producer defines an event:
@ Inject
Private javax. Enterprise. event. event <user> userevent;
To activate an event:
Userevent. Fire (User );
A message listener is a consumer. You only need to mark @ observes to process the event:
public void observeUserEvent(@Observes User user) { ... }
|
The introduction of the event model can provide a means for us to achieve the integration of business scenarios. See the zk CDI application:
@Named @SessionScoped public class HelloWorld extends GenericComposer implements Serializable { @Inject @ComponentId("guestName") Textbox guestName; @Inject @ComponentId("sayHelloBtn") Button sayHelloBtn; @Inject @ComponentId("helloWindow") Window helloWindow; public void sayHello(@Observes @Events("sayHelloBtn.onClick") MouseEvent evt) { helloWindow.setTitle("Hello " + guestName.getValue()); } }
|
However, this event mode is similar to the domain events provided by jdonframework based on the domain model, but there are still some differences. Currently, the CDI event mode is still a component (userevent) the user-driven domain model (Jf) is different from the event triggered by the domain model itself. The two are essentially different. It highlights the domain model as an important position of the Business Core.
Javaee6. In order to emphasize the important position of the technical architecture, we will inevitably compete with the business for the core position. This is what we users must pay attention to and cannot read the standard.
CDI also provides @ decorator and @ interceptor, which involves the concepts of AOP and dynamic components. If you are interested, you can study it carefully.
From: http://www.jdon.com/38322