Java custom Annotations (Annotation) __java

Source: Internet
Author: User

In the previous article, I introduced the principle and application scenario of annotations, and this article began to customize annotation.

annotation is different from class, Interface, Enum, the Java.lang.annotation.Annotation interface is automatically inherited when you use @interface to customize annotations, and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces.



1, Custom Myannotation

Package com.example.annotation;

Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;

@Target ({Elementtype.method})
@Target (Elementtype.method)
@Retention (retentionpolicy.runtime)
Public @interface myannotation {
	String name () default "ABC";	Specifies the default value
	int age ();
	Float[] Grade ();
}


@interface used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter. You can declare the default value of a parameter via default. The method return value type must be primitive (8 basic data type) type, class type, enum type, annotation type, or one-dimensional array of one of the preceding types. Here you can see meta annotations such as @retention and @target to declare the behavior of the annotation itself. The retention policy @Retention used to declare annotations, with three classes, runtime, and source, respectively, that the annotations are saved in class files, JVM runtime, and source code. It is only when declared as runtime that it is possible to obtain the annotation information through the reflection API at run time. @Target is used to declare what types of elements, such as types, methods, and fields, can be added to a note.


Note: Annotations are not inherited


2. Use Myannotation

@MyAnnotation (name= "AAA", age=24,grade={84.5f,90.0f}) public
	Boolean ispermitted () {return
		
		false;
	}


3. Using reflection to get the attribute value of the annotation

Package com.example.annotation;
Import Java.lang.reflect.Method;

Import java.sql.Connection;
	public class Jdbcutils {public static void main (string[] args) {getconnection (); @DbInfo (driver= "Com.mysql.jdbc.Driver", url= "Jdbc:mysql:///test", user= "root", password= "root") public static
		Connection getconnection () {//Class clazz = Jdbcutils.class;
			
			try {Class clazz = Class.forName ("Com.example.annotation.JdbcUtils");
			Method 1 Method[] methods = Clazz.getmethods ();
                	For (method Method:methods) {if (Method.isannotationpresent (Dbinfo.class)) {//To determine if there is a DbInfo annotation on this approach
                    
                	DbInfo info = (DbInfo) method.getannotation (Dbinfo.class); System.out.println ("method[" +method.getname () + "],driver[" +info.driver () + "],url[" +info.url () + "],user[" +
                Info.user () + "],password[" +info.password () + "]");
			}//Methods 2 method = Clazz.getmethod ("getconnection", null); DbInfo DbInfo = Method.getannOtation (Dbinfo.class); if (dbinfo!=null) {System.out.println ("method[" +method.getname () + "],driver[" +dbinfo.driver () + "],url[" +dbinfo.url
			() + "],user[" +dbinfo.user () + "],password[" +dbinfo.password () + "]");
		} catch (ClassNotFoundException e) {e.printstacktrace ();
		catch (Nosuchmethodexception e) {e.printstacktrace ();
		catch (SecurityException e) {e.printstacktrace ();
	return null;
 }
	
}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.