CDI (Contexts And Dependency Injection) is JavaEE 6 standard, a specification, the dependency injection IOC / DI rose to the container level, it provides component management core of the Java EE platform services injection, simplifying should be CDI's goal, so that everything It can be annotated be injected.
Contexts and concepts we discussed before business scenario is not the same in the DCI architecture, technical architecture scene contains a container of meaning, includes four scenes: request (event), session, application, page, and SEAM framework to expand the conversation and two business process context.
CDI models of JSF and EJB are using a relatively large changes, such as the impact on the JSF, JSF is the following one to display the Bean:
import javax.inject.Named;
@Named
public class MessageServerBean {
public String getMessage() {
return "Hello World!";
}
}
@Named to use the mark, it can be written to the tag in jsp page:
Message is : #{messageServerBean.message}<br>
Message Server Bean is : #{messageServerBean}
Reach the following output
Message is : Hello World!
Message Server Bean is : eedemo.MessageServerBean@xxxxxxx
That is, the @Named
mark is equivalent to the actual MessageServerBean
name eedemo.MessageServerBean
(of course you can also write an explicit name), you'll be able to direct messageServerBean
its getter
method of output.
Another feature is to CDI objects in the scene in a container life cycle marked out as follows:
@Named("itemProcessor")
@RequestScoped // represents the life cycle of request, request the end of each request, on the termination of life, you can have such as Session or Application
public class ItemProcessor {
@Inject
ItemDao ItemDao Private; // represents needs to be injected ItemDao
...
}
We have seen, in fact, the move has been widely used in the Spring or our Jdonframework, the actual dependency injection is an upgraded version. Automatic matching is an auto-wired injection, is not the kind of manual configuration Spring 1.x dependent kind. I am a revolutionary advantages 2005 article Ioc container proposed automatic injection of epoch-making significance. There was a time also this difference as jdonframework Spring 1.x and boasting a long time, and now have is the norm.
CDI also provides a method Producer, is the realization of the factory method, which is injected before the object, you can customize some of your own stuff.
public class PersonFactory {
@Produces
@RequestScoped
public Person createPerson() {
return new Person();
}
}
But Person
be injected into other needs Person
before places, createPerson
the first to be executed, in this method you do some preparation work before the injection.
CDI injection events Events also provided, so that asynchronous event model can be introduced in JavaEE.
Event news division producers and consumers, see Event-Listerner event listener mode article. News producers define an event:
@Inject
private javax.enterprise.event.Event<User> userEvent;
An event is activated:
userEvent.fire(user);
That is the message listener consumers, as long as the label @Observes, you can handle events emitted:
public void observeUserEvent(@Observes User user) {
...
}
The introduction of event patterns may provide a means for us to achieve business integration 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, based on Domain Events Domain Model pattern more similar to this event and Jdonframework provided, but there is some difference, it seems, CDI this event mode or components (userEvent) drive domain model (user), JF is different from the domain model itself emits events, both of which still have the essential difference, an important position in more prominent areas of the business model as the core, and JavaEE6 in order to emphasize the important position of its technology infrastructure, and business will inevitably compete for the heart, which we must pay attention to the user, death can not read standard.
CDI also provides @Decorator and @Interceptor, which involves the concept of AOP and dynamic components.
Original blog address: https: //blog.csdn.net/maijunjin/article/details/46120051