DEV Community

Mustafa  Çam
Mustafa Çam

Posted on

cdi ==> bean

@data
@AllArgsConstructor
@NoArgsConstructor
@builder

//cdi ==> bean
@Named(value = "namedtuto")
@ApplicationScoped
public class _01_Named {

private Long namedId;
private String namedData="Merhabalar Named Cdi Bean";
Enter fullscreen mode Exit fullscreen mode

}

Bu Java kodu, CDI (Contexts and Dependency Injection) ve Lombok kütüphanesini kullanarak bir bean tanımlıyor. Açıklamalarıyla birlikte her bir parçanın ne işe yaradığını inceleyelim:

  • @Data: Lombok kütüphanesinden bir annotation'dır ve sınıf için getter, setter, toString, equals ve hashCode metodlarını otomatik olarak oluşturur¹⁵.
  • @AllArgsConstructor: Yine Lombok'tan bir annotation olup, sınıfın tüm alanları için parametre alan bir constructor oluşturur¹.
  • @NoArgsConstructor: Sınıf için parametresiz bir constructor oluşturur. Bu, özellikle JPA gibi bazı Java EE teknolojileri tarafından gereklidir¹.
  • @Builder: Lombok ile, nesneleri adım adım oluşturmak için kullanılan builder pattern'ini kolayca uygulamak için kullanılır⁶.

CDI ile ilgili annotation'lar:

  • @Named(value = "namedtuto"): CDI'da, bean'in isimlendirilmesini sağlar ve bu isimle bean'e erişilebilir hale gelir[^10^].
  • @ApplicationScoped: Bean'in uygulama kapsamında olduğunu belirtir, yani uygulama çalıştığı sürece tek bir instance'ı olur ve tüm uygulama boyunca paylaşılır¹⁸.

Bu kod parçası, namedId ve namedData adında iki özel alana sahip _01_Named isimli bir sınıf tanımlar. namedData alanı "Merhabalar Named Cdi Bean" string'i ile başlatılmıştır. Bu sınıf, CDI tarafından yönetilen bir bean olarak işlev görür ve Lombok sayesinde ekstra boilerplate kod yazmaya gerek kalmadan çeşitli yardımcı metodlara otomatik olarak sahip olur. Bu bean, namedtuto ismiyle uygulama genelinde erişilebilir ve uygulama kapsamında yaşar, yani uygulama çalıştığı sürece aynı instance'ı kullanılır. Bu yapı, özellikle web uygulamalarında ve enterprise Java uygulamalarında sıkça kullanılır.

Kaynak: Bing ile konuşma, 18.05.2024
(1) @data - Project Lombok. https://projectlombok.org/features/Data.
(2) @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor. https://projectlombok.org/features/constructor.
(3) Using Lombok's @builder Annotation | Baeldung. https://www.baeldung.com/lombok-builder.
(4) java - What is javax.inject.Named annotation supposed to be used for .... https://stackoverflow.com/questions/5415261/what-is-javax-inject-named-annotation-supposed-to-be-used-for.
(5) java - What is the difference between @ApplicationScoped and @Singleton .... https://stackoverflow.com/questions/4555844/what-is-the-difference-between-applicationscoped-and-singleton-scopes-in-cdi.
(6) Why to use @AllArgsConstructor and @NoArgsConstructor together over an .... https://stackoverflow.com/questions/68314072/why-to-use-allargsconstructor-and-noargsconstructor-together-over-an-entity.
(7) What is the @NoArgsConstructor annotation in Lombok? - Educative. https://www.educative.io/answers/what-is-the-noargsconstructor-annotation-in-lombok.
(8) @AllArgsConstructor vs @NoArgsConstructor vs @RequiredArgsConstructor .... https://dailycodework.com/lombok-annotations-explained/.
(9) Annotation Type NoArgsConstructor - Project Lombok. https://projectlombok.org/api/lombok/NoArgsConstructor.
(10) @builder - Project Lombok. https://projectlombok.org/features/Builder.
(11) @builder annotation in spring boot | Code Ease. https://www.codeease.net/programming/java/builder-annotation-in-spring-boot.
(12) Lombok @builder with Examples- HowToDoInJava. https://howtodoinjava.com/lombok/lombok-builder-annotation/.
(13) An Introduction to CDI in Java | Baeldung. https://www.baeldung.com/java-ee-cdi.
(14) Java CDI Dependency Injection Example | Burak Aktas. http://buraktas.com/java-cdi-dependency-injection-example/.
(15) Creating Named beans with annotations (JSF 2.0) - IBM. https://www.ibm.com/docs/SSRTLW_9.6.0/com.ibm.etools.jsf.doc/topics/tcrt_facesNamed_bean_annot.html.
(16) Giving Beans EL Names - The Java EE 6 Tutorial - Oracle. https://docs.oracle.com/javaee/6/tutorial/doc/gjbak.html.
(17) @data Lombok Annotation Example - Java Guides. https://www.javaguides.net/2019/03/project-lombok-data-annotation-example.html.
(18) Using Data annotation on Java DTO class - Stack Overflow. https://stackoverflow.com/questions/51820133/using-data-annotation-on-java-dto-class.
(19) ApplicationScoped (Java(TM) EE 7 Specification APIs) - Oracle. https://docs.oracle.com/javaee/7/api/javax/enterprise/context/ApplicationScoped.html.
(20) ApplicationScope (Spring Framework 6.1.6 API). https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/annotation/ApplicationScope.html.
(21) ApplicationScope (Spring Framework API) - Javadoc - Pleiades. https://spring.pleiades.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/annotation/ApplicationScope.html.
(22) ApplicationScoped (Java EE 6 ) - Oracle. https://docs.oracle.com/javaee/6/api/javax/faces/bean/ApplicationScoped.html.
(23) Difference Between Lombok @AllArgsConstructor ... - Baeldung. https://www.baeldung.com/java-lombok-constructor-annotations-comparison.
(24) What is the @AllArgsConstructor annotation in Lombok? - Educative. https://www.educative.io/answers/what-is-the-allargsconstructor-annotation-in-lombok.
(25) java 8 - @AllArgsConstructor and Constructor Injection with Spring: is .... https://stackoverflow.com/questions/62631503/allargsconstructor-and-constructor-injection-with-spring-is-private-final-need.
(26) undefined. https://mvnrepository.com/artifact/org.projectlombok/lombok.
(27) undefined. https://projectlombok.org/download.
(28) undefined. http://download.oracle.com/javaee/6/api/javax/inject/Named.html.
(29) undefined. https://dzone.com/articles/java-ee6-cdi-named-components.
(30) undefined. http://java.sun.com/xml/ns/javaee.
(31) undefined. http://www.w3.org/2001/XMLSchema-instance.
(32) undefined. http://java.sun.com/xml/ns/javaee/beans_1_0.xsd.

Top comments (0)