Java explanation

In the past few days, I learned about Spring3. x and found that many frameworks now use the Java annotation feature, and then did some background research on Java annotations.

First of all, annotation and annotation are not the same value and not the same.

Note that we usually interpret the code, that is, “//…” and “/*………………./”, I believe everyone is familiar with it, so I won’t I say a lot here.

As for annotations, we can say that this is a very useful technology. What is an annotation? In fact, an annotation is a description and description of the code, and it can be read. Code description. (personal understanding)

In the process of writing code (such as Eclipse) in some IDEs, we may encounter some code with wave lines or a line in the middle telling you that this method cannot be protected. This method may be in the future. reminder like deletion in release,

Actually, this is a summary to help you.

Because after Java5.0 (JDK1.5), Java builtDeprecated Annotation by default. Its presence is the above functions. Also, there’s @oupride, @suppresswarninengs for the other two functions, all, all, all, all, all, all, all, all, all, all, all, just explore yourself.

The power of annotation is that we can customize the annotation we need!

First of all, annotations are the same as the class and interface, and they are all of type

Using a Note definition is similar to the Definition interface. The difference is that there is another @:

interface MyAnnotation {

string() defaults to “NULL”;

}

This defines which note is yours.

The annotation itself is also code, of course there are annotations to describe the annotation.

It’s a little strange to read, but it’s an important feature. Annotations – describe scale range and survival range.

There are four descriptions for annotations, including:

@target

Myannotation() // This is wrong

public class annotations test {

Myannotation() // That’s right

public void test() {

}

}

The other three types are:

@save

This means that annotation information is preserved at any level. Optional suspension policy options include:

Source: The annotation will be ignored by the compiler

class: note can be used in class files, but will be ignored by the VM

Runtime: The virtual machine will save the annotations at runtime, so you can read the annotation information using the reflection mechanism.

@document

Include annotation in javadoc

@heir

Allow subclasses to inherit annotations in the parent class

What is the benefit of custom annotations? How to use it?

To use the annotation I’m using an example I wrote and are there any bad places to shoot the light – #

First, create a new custom annotation

package my.annotation.i;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "NULL";
}

Then write an annotation test class

package my.annotation.impl;

import my.annotation.i.MyAnnotation;


public class AnnotationTest {
@MyAnnotation("This is method 1")
	public String method1(String s){
	System.out.println("method1 is invoked,params is "+s);
	return s;
	}
   public String method2(String s){
	   System.out.println("method2 is invoked,params is "+s);
	   return s;
   }
   @MyAnnotation("This is method 3")
   public String method3(String s){
	   System.out.println("method3 is invoked,params is "+s);
	   return s;
   }
}

Finally, write to the client, the summary will show your skills

package my.annotation.client;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import my.annotation.i.MyAnnotation;
import my.annotation.impl.AnnotationTest;
public class Client {
public static void main(String[] args) throws Exception{
	Class cs = Class.forName("my.annotation.impl.AnnotationTest");
	        Method[] methods = cs.getDeclaredMethods();
	        for(Method method:methods){
	        	if(method.isAnnotationPresent(MyAnnotation.class)){
	        		MyAnnotation aTest = method.getAnnotation(MyAnnotation.class);
	        	    method.invoke(cs.newInstance(),aTest.value());
	        	}
	        }
}
}

The conclusion is as follows:

Method 1 has been called, the parameters are this method 1
method3 has been called, the parameters are this method3

This is my initial understanding of the Java annotation, which may be wrong, everyone understands – #

Leave a Comment