How to write Java annotations?

Java annotations

In this article we will discuss about annotations and how they can be used in practical programming. First, we will discuss some conceptual ideas and then jump into coding to write an example program using annotations.

Motivation:

In Java programming, Farklı elementlerin davranışı bildirmek için birkaç yöntem vardır,,en,yöntemlerle gibi,,en,üyeler vb,,en,Vakaların çoğunda bu beyanlar bazı dış dosyalarında tanımlanmıştır,,en,özellikleri vb dosya,,en,Ve sonra bu dosyalar da idam etmek programda adlandırılır,,en,Ek açıklamanın durumda,,en,yaklaşım tamamen farklı,,en,Ek açıklamalar meta verilerdir ve bu belirli bir biçimde yazılır,,en,Bunlar ek açıklama elemanları Java programı kendi içinde yazılır,,en,Herhangi bir harici dosyaları başvurmak için gerek yoktur,,en,bu büyük bir avantajdır,,en,Bu ek açıklamalar kullanımı kolay ve uygulamaya kadar esnek,,en,Bir sonraki bölümde bu nasıl uygulanabileceğini göreceksiniz,,en,Java açıklama nedir,,en,Bir Java ek açıklama bir Java programının bir meta olduğunu,,en,sadece konuşarak,,en,Java ek açıklama bir Java programı hakkında bilgi sağlar,,en (like methods, classes, members etc). In most of the cases these declarations are defined in some external files (XML, properties file etc). And then those files are referred in the program to get it executed.

In case of annotation, the approach is totally different. Annotations are meta-data and those are written in a specific format. These annotation elements are written within the Java program itself. So, there is no need to refer any external files, which is a big advantage. These annotations are easy to use and flexible enough to implement.

In the next section we will see how these can be implemented.








What is Java annotation?

A Java annotation is a metadata of a Java program. Or, simply speaking, a Java annotation provides information about a Java program. Çoğu durumda,,en,doğrudan program veya kod etkilemez,,en,Neden Java ek açıklamalar kullanılır,,en,Bir Java ek açıklama aşağıdaki amaçlar için kullanılabilir,,en,derleyici talimat verilmesi,,en,derleyici makine tarafından okunabilir talimatlar içine kod dönüştürür zaman,,en,ek açıklamalar derleyici tür talimatları sağlayabilir,,en,derleme sonra,,en,Ek açıklamalar kodunda mevcut değildir,,en,oluşturucu talimat verilmesi,,en,Bugünlerde,,en,Apache Maven veya Apache Ant gibi araçlar kod oluşturmak için izin verir,,en,Açıklamalar Yapım araçları için yararlı olabilir,,en,inşa etmek araçları kendi görevler hakkında gittiğinizde üreten kaynak kodunu içerir hangi,,en,XML dosyalarını üreten,,en,dağıtım tanımlayıcıları,,en,Bir JAR dosyası içine kodları ve dosyaları ambalaj,,en,o kaynak kodu veya dosyaları oluşturma konulu girişler için ek açıklamaları arar,,en, it does not directly influence the program or code.

Why Java annotations are used?

A Java annotation can be used for the following purposes:

  • Providing instructions to the compiler

When the compiler converts the code into machine-readable instructions, annotations can provide the compiler such instructions. Usually, after compilation, the annotations are not present in the code.

  • Providing instructions to the builder

These days, you would let tools like Apache Maven or Apache Ant to build the code. Annotations can be useful for build tools. When the build tools go about their tasks which include generating source code, generating XML files (e.g. deployment descriptors), packaging codes and files into a JAR file, it looks for the annotations for inputs on generating source code or files.

  • Yazılım çalışma zamanı talimat verilmesi,,en,Bu tür açıklamalar, Java Yansımalar ile erişilebilir rağmen yazılım çalışma girdi sağlamak için ek açıklama tanımlamak mümkündür,,en,Farklı bir araç,,en,Ek açıklamalar, önceden tanımlanmış ve özel ikisi de olabilir,,en,özel açıklamalar birçok türde olabileceği gibi,,en,Önceden tanımlanmış ek açıklamalar şu üç tiptir,,en,@Deprecated,,en,Ek açıklama bir sınıf olduğunu gösterir,,en,Kullanılan artık yöntem veya alan,,en,Ek açıklama, yöntemde bir sınıf Bir üst sınıfta herhangi bir yöntem uyuşmadığını gösterir,,en,Bir uyumsuzluk durumunda,,en,derleyici bir hata verir,,en,@SuppressWarnings,,en,Ek açıklama derleyici tarafından atılan uyarıları bastırır,,en,Bir yöntem, bir kaldırılmış bir yöntem çağırır,,en,derleyici bir uyarı atıyor,,en,Ek açıklama tür uyarıları ve daha bastırır,,en,İlk proje oluşturma,,en

It is possible to define annotations for providing inputs to software runtime although such annotations can be accessed with Java Reflections, a different tool.

Annotations can be both predefined and custom. While custom annotations can be of many types, predefined annotations are of the following three types.








@Deprecated annotation indicates that a class, method or field that is no longer in use.

@ Override annotation indicates that a class in a method does not match any method in a superclass. In case of a mismatch, the compiler throws an error.

@SuppressWarnings annotation suppresses warnings thrown by the compiler. For example, if a method calls a deprecated method, the compiler throws a warning. The annotation suppresses such warnings and more.

Creating our first project:

Bu bölümde Eclipse IDE kullanılarak bir proje yaratacak,,en,Ek açıklama gerçek ortamda nasıl çalıştığını kod örneği gösterecektir,,en,içine taşımadan önce uygulamalı kodlama biz çevre hazır hale getirmek için kurulum adımlarını tamamlamak için gereken,,en,Aşağıdaki kurulum detaylar,,en,Java geliştirme kitini kurma,,en,İndir,,en,Oracle sitesinden,,en,yüklemek ve yapılandırmak,,en,Sonra YOLU ve java_home Ortam değişkenlerini ayarlamak,,en,Eclipse IDE kurma,,en,dan Eclipse İndir,,en,resmi internet sitesi,,en,Kurun ve PATH değişkeni ayarlamak,,en,indirin açıklama paketleri,,en,bahar,,en,kütüphaneler,,en,Örneğimizde Bahar açıklama paketleri kullanıyor olacak,,en,org.springframework.context.jar,,en,Yukarıdaki bağlantıdan ve koyun,,en,lib,,en,Eclipse IDE klasör, aşağıda gösterildiği gibi,,en,Şekil 1,,en,lib klasöründe Ek Açıklama Kavanozlar,,en. The code sample will show how annotation works in a real environment. Before moving into hands-on coding we need to complete the installation steps to make the environment ready.

Following are the installation details.

Step 1: Setting up Java development kit

Download JDK from the Oracle site, install and configure it. Then set the PATH and JAVA_HOME environment variables.

Step 2: Setting up Eclipse IDE

Download Eclipse from the official website. Install it and set the PATH variable.

Step 3: Download annotation packages from spring libraries.

In our example we will be using Spring annotation packages. Download org.springframework.context.jar from the above link and put it in the lib folder in Eclipse IDE as shown below.

Spring packages

Spring packages

Figure1: Annotation Jars in lib folder

Şimdi çevremiz ek açıklamalarla Java programlar yazmak için hazır,,en,Bize Eclipse IDE kullanarak ilk projesi yaratalım,,en,Eclipse bir proje oluşturun ve olarak paket adını ayarlamak,,en,com.techalpine.annotation,,en,aşağıda gösterildiği gibi bu paket altında üç Java sınıfları oluşturmak,,en,Şekil 2,,en,Proje yapısı,,en,Üç sınıflar BeanAnnotation.java vardır,,id,BeanAnnotationConfig.java ve BeanAnnotationMain.java,,id,Bu alıcı ve ayarlayıcı yöntemleri ile fasulye sınıftır,,en,paket com.techalpine.annotation,,en,public class BeanAnnotation,,id,Özel String messagetxt,,en,public void setMessagetxt,,en,Dize messagetxt,,en,this.messagetxt = messagetxt,,en,public void getMessagetxt,,en,Bu mesajdır,,en,Sonraki yapılandırma sınıfı @ ile,,en,ve @,,en,Fasulye,,en,ek açıklamalar,,en,Bu ek açıklamalar fasulye belirlemek ve bağlayıcı için kabı bilgilendirecektir,,en,Bu yapılandırma sınıftır,,en.

Let us create out first project by using Eclipse IDE.

Create a project in Eclipse and set the package name as com.techalpine.annotation. Under this package create three Java classes as shown below.

Project Structure

Project Structure

Figure2: Project structure

Three classes are BeanAnnotation.java, BeanAnnotationConfig.java and BeanAnnotationMain.java

Listing1: This is the bean class with getter and setter methods

[kod]

package com.techalpine.annotation;

public class BeanAnnotation {

private String messagetxt;

public void setMessagetxt(String messagetxt){

this.messagetxt = messagetxt;

}

public void getMessagetxt(){

System.out.println(“This is the message : ” + messagetxt);

}

}

[/kod]

Next is the configuration class with @Configuration and @Bean annotations. These annotations will identify the bean and inform the container for binding.

Listing2: This is the configuration class

[kod]

package com.techalpine.annotation;

org.springframework.context.annotation aktarın. *,,en,@Configuration,,en,public class BeanAnnotationConfig,,id,@Fasulye,,en,BeanAnnotation kamu beanannotate,,id,Yeni BeanAnnotation dönmek,,id,Sonraki ek açıklamaları test etmek için ana sınıftır ve nasıl çalıştığını,,en,Bu ana sınıftır,,en,public class BeanAnnotationMain,,id,ApplicationContext CTX =,,en,yeni AnnotationConfigApplicationContext,,en,BeanAnnotationConfig.class,,id,BeanAnnotation beanannotate = ctx.getBean,,id,BeanAnnotation.class,,id,beanannotate.setMessagetxt,,id,Ek Açıklama iyi çalışıyor,,en,beanannotate.getMessagetxt,,id,projeyi yürütme,,en,Proje yapısı tamamlandıktan ve yürütmek hazır,,en,aşağıda gösterildiği gibi Şimdi uygulamayı çalıştırmak,,en,Bu tek başına bir Java programı olacak,,en,aşağıda gösterildiği gibi bu yüzden bir Java uygulaması olarak çalıştırmak zorundadır,,en,Figure3,,en,uygulamayı çalıştırın,,en,Ana program çalıştırılır kez,,en,aşağıdaki çıktıyı gösterecektir,,en,Şekil 4 Bu,,en,konsolda çıktı gösteriliyor,,en;

@Configuration

public class BeanAnnotationConfig {

@Bean

public BeanAnnotation beanannotate(){

return new BeanAnnotation();

}

}

[/kod]







Next is the main class to test the annotations and how it works.

Listing3: This is the main class

[kod]

package com.techalpine.annotation;

import org.springframework.context.ApplicationContext;

org.springframework.context.annotation aktarın. *,,en,@Configuration,,en,public class BeanAnnotationConfig,,id,@Fasulye,,en,BeanAnnotation kamu beanannotate,,id,Yeni BeanAnnotation dönmek,,id,Sonraki ek açıklamaları test etmek için ana sınıftır ve nasıl çalıştığını,,en,Bu ana sınıftır,,en,public class BeanAnnotationMain,,id,ApplicationContext CTX =,,en,yeni AnnotationConfigApplicationContext,,en,BeanAnnotationConfig.class,,id,BeanAnnotation beanannotate = ctx.getBean,,id,BeanAnnotation.class,,id,beanannotate.setMessagetxt,,id,Ek Açıklama iyi çalışıyor,,en,beanannotate.getMessagetxt,,id,projeyi yürütme,,en,Proje yapısı tamamlandıktan ve yürütmek hazır,,en,aşağıda gösterildiği gibi Şimdi uygulamayı çalıştırmak,,en,Bu tek başına bir Java programı olacak,,en,aşağıda gösterildiği gibi bu yüzden bir Java uygulaması olarak çalıştırmak zorundadır,,en,Figure3,,en,uygulamayı çalıştırın,,en,Ana program çalıştırılır kez,,en,aşağıdaki çıktıyı gösterecektir,,en,Şekil 4 Bu,,en,konsolda çıktı gösteriliyor,,en;

public class BeanAnnotationMain {

public static void main(Dizi[] args) {

ApplicationContext ctx =

new AnnotationConfigApplicationContext(BeanAnnotationConfig.class);

BeanAnnotation beanannotate = ctx.getBean(BeanAnnotation.class);

beanannotate.setMessagetxt(“Hello, Annotation is working fine!”);

beanannotate.getMessagetxt();

}

}

[/kod]








Executing the project:

The project structure is complete and it is ready to execute. Now run the application as shown below. It will be a stand-alone Java program, so it has to be run as a Java application as shown below.

Run application

Run application

Figure3: Run the application

Once the main program is executed, it will show the following output.

Output

Output

Figure4: Showing output on the console

Sonuç:

Bu yazıda ek açıklamada kavramlarını ele aldık ve nasıl uygulanabilir,,en,o Ek açıklamalar içeren Java programları yazmak için kullanıcılara yardımcı olacak Umut,,en. Hope it will help the users to write Java programs with annotations.

 

Tagged on: ,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share