The course is part of this learning path
This course explores Context and Dependency Injection (CDI) within Java EE. We'll look at what it is, as well as beans in CDI and CDI qualifiers.
- Learn the fundamentals of CDI
- Learn about beans and qualifiers
This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.
Basic knowledge of Java programming.
Hello dear friends. In this video, we will examine Qualifiers. Let's begin. Qualifier allows you to uniquely specify a bean to be injected among its multiple implementations. For example, this code snip declares a new Qualifier, Fancy or this code snip defines a new implementation of the Greeting interface and injects it in the Greeting service by specifying Fancy as the Qualifier. This removes any direct dependency to any particular implementation of the interface. Qualifiers may take parameters for further discrimination. Multiple Qualifiers may be specified at an injection point. Now, let's look at the built-in Qualifiers. There are four built-in Qualifiers: Named, default, any, and new. @Named qualifier is string-based qualifier required for usage in expression language. @Default is the default qualifier on all beans without an explicit Qualifier except @Named. @Any is the default Qualifier on all beans except @New.
@New Qualifier allows the application to obtain a new instance independent of the declared scope. Now, that's all I have for Qualifiers. Let's continue with scopes. For a web application to use a bean that injects another bean class, the bean needs to be able to hold state over the duration of the user's interaction with the application. The way to define this state is to give the bean a scope. You can give any object any scope depending on how you're using it. There are four predefined scopes and one default scope. These are: RequestScoped, SessionScoped, ApplicationScoped, ConversationScoped, and Dependent. With RequestScoped, a bean is scoped to a request. The bean is available during a single request and destroyed when the request is complete. With SessionScoped, a bean is scoped to a session. The bean is shared between all requests that occur in the same HTTP session, hold state throughout the session and is destroyed when the HTTP session times out or is invalidated.
With ApplicationScoped, a bean is scoped to an application. The bean is created when the application is started, hold state throughout the application, and is destroyed when the application is shut down. With ConversationScoped, a bean is scoped to a conversation and is of two types as transient or long-running. By default, a bean in this scope is transient, is created with a JSF request, and is destroyed at the end of the request. A transient conversation can be converted to a long running one using conversation.begin. This long running conversation can be ended using conversation.end. All long running conversations are scoped to a particular HTTP servlet session and may be propagated to other JSF requests. Multiple parallel conversations can run within a session, each uniquely identified by a string valued identifier that is either set by the application or generated by the container. This allows multiple tabs in a browser to hold state corresponding to a conversation unlike session cookies that are shared across tabs. With Dependent, a bean belongs to the dependent pseudo scope.
This is the default scope of the bean that does not explicitly declare a scope. The Request, Session, and Application scopes are defined by both JSR 299 and the JavaServer Faces API. The last two are defined by JSR 299. All predefined scopes except dependent are contextual scopes. CDI places beans of contextual scope in the context whose lifecycle is defined by the Java EE specifications. For example, a session context and its beans exist during the lifetime of an HTTP session. Injected references to the beans are contextually aware. The references always apply to the bean that is associated with the context for the thread that is making the reference. The CDI container ensures that the objects are created and injected at the correct time as determined by the scope that is specified for these objects. A scope gives an object a well defined lifecycle context. A scoped object can be automatically created when it is needed and automatically destroyed when the context in which it was created ends. Moreover, its state is automatically shared by any clients that execute in the same context.
Java EE components such as servlets and enterprise beans, and Java beans components do not, by definition, have a well defined scope. However, if you create a Java EE component that is a managed bean, it becomes a scoped object which exists in a well defined lifecycle context. That's all I have for now. We may add new sections to this topic in the future, but for now, that is enough. We have now finished discussing CDI with this video. In the next video, we will start to examine Java Persistence API also known as JPA. See you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.