20 Ocak 2022 Perşembe

Jakarta EE @ApplicationScoped Anotasyonu

Giriş
Eski kodlarda şu satırı dahil ederiz.
import javax.ejb.ApplicationScoped;
Şu satırı dahil ederiz
import jakarta.enterprise.context.ApplicationScoped;
Normalde Jakarta EE CDI ile herhangi bir anotasyon kullanmaya gerek yok. Açıklaması şöyle
The requirements for a CDI bean are very minimal: a default constructor, implicitly or explicitly defined, and a concrete class is all that is needed. There is no need to use any kind of annotation, to indicate it can be handled through the CDI subsystem.
Ancak bazen CDI bean'lerine kapsam (scope) vermek gerekir. Çünkü bunu yapmazsak, CDI bean her seferinden yeniden yaratılır. Açıklaması şöyle
Most of the time, an annotation is placed on the class to indicate when the bean can be disposed of, and how many copies of the bean can be created. In the absence of such an annotation, the bean is created every time an 'Injection' is requested and disposed of when the other class is disposed of.
Verilebilecek kapsam anotasyonları 3 tane. Açıklaması şöyle
@jakarta.enterprise.context.ApplicationScoped: one instance for the entire application.

@jakarta.enterprise.context.SessionScoped: an instance is typically linked with a user Session like the HTTP Session.

@jakarta.enterprise.context.RequestScoped: an instance for each user request.

Since the ApplicationScoped can't hold any user or request specific information, it is ideal for providing functionality to your application, also known as services.

SessionScoped is typical for holding user information (like logged in user) and RequestScoped can be used to keep information related to the specific user request. 
@Application anotasyonu state tutmayan singleton bir bean yaratır. Açıklaması şöyle
Since the ApplicationScoped can't hold any user or request specific information, it is ideal for providing functionality to your application, also known as services.

SessionScoped is typical for holding user information (like logged in user) and RequestScoped can be used to keep information related to the specific user request. 
Örnek
Şöyle yaparız
@ApplicationScoped
public class GreetingService {
...
}

Hiç yorum yok:

Yorum Gönder

Bean Validation @GroupSequence Anotasyonu

Örnek Elimizde şöyle bir kod olsun public class SampleRequest {   @NotNull   LocalDate startDate;   @NotNull   LocalDate endDate;   @AssertT...