Conas a scríobh nótaí Java?

Java annotations

San Airteagal seo, beimid ag plé faoi nótaí agus conas is féidir iad a úsáid i gcláir phraiticiúil. First, beidh muid ag plé roinnt smaointe coincheapúla agus ansin léim isteach códú a scríobh clár mar shampla ag baint úsáide as nótaí.

spreagadh:

I gcláir Java, tá roinnt modhanna a dhearbhú go bhfuil an iompar na n-eilimintí éagsúla (cosúil le modhanna, ranganna, baill srl). Sa chuid is mó de na cásanna na dearbhuithe a shainmhíniú i roinnt comhaid seachtracha (XML, Airíonna comhad srl). Agus ansin na comhaid Tagraítear sa chlár a fháil dó chun báis.

I gcás anótáil, Is é an cur chuige go hiomlán difriúil. Tá nótaí meta-sonraí agus iad siúd atá scríofa i bhformáid ar leith. Tá na heilimintí anótáil atá scríofa laistigh de chlár Java féin. So, níl aon ghá tagairt d'aon chomhaid seachtracha, is buntáiste mór. 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. 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. 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.

  • Providing instructions to the software runtime

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.

@ Sáraigh 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

Íoslódáil Eclipse ó láithreán gréasáin oifigiúil. Suiteáil é agus leag an athróg PATH.

Step 3: Pacáistí Download anótáil ó earrach leabharlanna.

In ár sampla beidh muid ag baint úsáide as pacáistí anótáil Earraigh. Download org.springframework.context.jar as an nasc thuas agus é a chur sa lib fillteán i Eclipse IDE mar a thaispeántar thíos.

Spring packages

pacáistí Earraigh

Figure1: Prócaí Annotation i bhfillteán lib

Anois tá ár dtimpeallacht réidh a scríobh cláir Java le nótaí.

Lig dúinn a chruthú amach chéad tionscadal trí úsáid a bhaint Eclipse IDE.

Cruthaigh tionscadal i Eclipse agus leag an t-ainm pacáiste mar com.techalpine.annotation. Faoin bpacáiste a chruthú trí rang Java mar a thaispeántar thíos.

Project Structure

Struchtúr an Tionscadail

Figure2: struchtúr an Tionscadail

Tá trí rang BeanAnnotation.java, BeanAnnotationConfig.java agus BeanAnnotationMain.java

Listing1: Is é seo an rang Bean le modhanna getter agus setter

[Cód]

com.techalpine.annotation pacáiste;

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);

}

}

[/Cód]

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

[Cód]

com.techalpine.annotation pacáiste;

import org.springframework.context.annotation.*;

@Configuration

public class BeanAnnotationConfig {

@Bean

public BeanAnnotation beanannotate(){

return new BeanAnnotation();

}

}

[/Cód]







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

Listing3: This is the main class

[Cód]

com.techalpine.annotation pacáiste;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.*;

public class BeanAnnotationMain {

statach neamhní príomh phoiblí(Teaghrán[] comhbhrí leis an rogha) {

ApplicationContext ctx =

new AnnotationConfigApplicationContext(BeanAnnotationConfig.class);

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

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

beanannotate.getMessagetxt();

}

}

[/Cód]








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

Conclúid:

In this article we have discussed the concepts of annotation and how it can be implemented. 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