The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on CDI.
- Understand contexts and dependency injection (CDI)
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
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. Fo 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. At name qualifier is a string based qualifier required for usage in expression language. At default is the default qualifier on all beans without an explicit qualifier except at named.
At any is the default qualifier on all beans except at new. At 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 the state is to give the bean a scope. You can give any object any scope depending on how you are 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 the 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 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 scope. The request, session and application scopes are defined by both JSRR 299 and the Java server 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 life cycle 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 JavaBeans 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 I think you got it. We have now finished discussing CDI with this video, so I'll 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.