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, there are several methods to declare the behavior of different elements (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. və ya, simply speaking, a Java annotation provides information about a Java program. In most cases, 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. adətən, 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. Ek Notlar build alətlər üçün faydalı ola bilər. build tools onların vəzifələri haqqında getmək zaman yaradan mənbə kodu daxildir, XML faylları yaradan (e.g. yerləşdirilməsi Deskriptorlar), bir JAR fayl kodları və faylları qablaşdırma, Bu mənbə kodu və ya faylları yaradan giriş üçün Ek notlar üçün görünür.

  • proqram uzunluğu təlimat təmin

Belə notları Java Reflections ilə əldə edilə bilər, baxmayaraq ki, proqram uzunluğu üçün giriş təmin notları müəyyən etmək mümkündür, fərqli bir vasitə.

Ek Notlar əvvəlcədən təyin edilmiş və xüsusi, həm də ola bilər. Xüsusi notları çox növləri ola bilər baxmayaraq, əvvəlcədən təyin edilmiş notları Aşağıdakı üç növ var.








@Deprecated annotasiya sinif göstərir ki, istifadə artıq metodu və ya sahə.

@ Rusiyada yüklenme 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:

In this section we will create a project by using Eclipse IDE. 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

olan Eclipse Download rəsmi internet səhifəsi. Quraşdırma və PATH dəyişən müəyyən.

Step 3: Download annotasiya paketləri yaz kitabxana.

Bizim nümunədə biz Spring annotasiya paketləri istifadə olunacaq. Download org.springframework.context.jar yuxarıda link və qoyun lib Eclipse IDE qovluq aşağıda göstərildiyi kimi.

Spring packages

Spring paketləri

Figure1: lib qovluq Annotasiya Kavanozlar

İndi bizim ətraf mühit notları ilə Java proqramları yazmaq üçün hazır.

Bizə Eclipse IDE istifadə edərək ilk layihəni həyata yaratmaq edək.

Eclipse bir layihə yaratmaq və paket adı müəyyən com.techalpine.annotation. aşağıda göstərildiyi kimi bu paketi çərçivəsində üç Java dərsləri yaratmaq.

Project Structure

Layihə strukturu

figure2: Layihə strukturu

Üç dərsləri BeanAnnotation.java var, BeanAnnotationConfig.java və BeanAnnotationMain.java

Listing1: Bu törədici və tula üsulları ilə lobya sinif

[kod]

paketi 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]

paketi com.techalpine.annotation;

import org.springframework.context.annotation.*;

@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]

paketi com.techalpine.annotation;

idxal org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.*;

public class BeanAnnotationMain {

ictimai statik void main(Sim[] 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:

Layihə strukturu tam və icra etməyə hazırdır. aşağıda göstərilən İndi proqram run. Bu bağımsız bir Java proqramı olacaq, aşağıda göstərildiyi kimi belə bir Java proqram kimi run var.

Run application

Run proqram

Figure3: proqram Run

Əsas proqram icra sonra, aşağıdakı çıxış göstərəcək.

Output

buraxılış

Figure4: konsol çıxış göstərən

Nəticə:

Bu yazıda annotasiya anlayışlar müzakirə etdik və necə həyata keçirilə bilər. Bu notları ilə Java proqramları yazmaq üçün kömək edəcək.

 

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