On Java annotation and dynamic proxy

This article mainly introduces some knowledge related to annotations and dynamic proxies in Java. Next, let's take a look at the specific content.

Annotation

In fact, it is a special tag in the code, which is used to replace the configuration file. In other words, the traditional way tells the class how to run through the configuration file. With annotation technology, developers can tell the class how to run through annotations.

1. Three basic annotations:

Override: restrict overriding parent class methods. This annotation can only be used for methods

Deprecated: used to indicate that a program element (class, method, etc.) is obsolete

Suppresswarnings: suppresses compiler warnings

2. Custom annotation uses the @ interface keyword

The information of the configuration file is described by attributes in the annotation

Example:

Method with default value: string name() default "XXX"

Annotation properties can only be of the following types:

String type, 8 basic data types, class type, enumeration type, annotation type, and one-dimensional array of the above types

Special attribute value: if there is an attribute named value in the annotation, the value = part can be omitted when using the annotation, such as @ myannotation ("XXX")

Special attribute value [];

3. Meta annotation refers to an annotation that modifies an annotation.

The following meta annotations are defined in the JDK:

Retention: can only be used to decorate an annotation definition to specify the fields that can be reserved by the annotation, @ retention contains a member variable of retentionpolicy type, which can be used to specify the fields.

RetentionPolicy. Class: the compiler will record the annotations in the class file When running Java programs, the JVM does not retain annotations This is the default

RetentionPolicy. Runtime: the compiler will record comments in the class file When running a java program, the JVM keeps annotations The program can obtain the annotation through reflection (so when customizing an annotation, it is generally necessary to add this meta annotation)

RetentionPolicy. Source: the compiler directly discards comments for this strategy

4. @ target: specifies which member of the class the annotation is used to decorate@ Target contains a member variable named value and of type ElementType.

@Documented: used to specify that the annotation class modified by the meta annotation will be extracted into a document by the Javadoc tool

@Inherited: the annotation modified by it will be inherited If a class uses an annotation modified by @ inherited, its subclasses will automatically have the annotation

Dynamic agent

Java provides a proxy class. Calling its newinstance method can generate a proxy object of an object. When using this method to generate a proxy object, three parameters are required:

1. Which class loader is used to generate proxy objects

2. The proxy object of which object is generated is specified through the interface

3. What to do in the method of the generated proxy object is specified by the developer writing the implementation of the handler interface.

There are 2 things that beginners must understand or do not understand that they must remember:

When the proxy class is responsible for creating a proxy object, if a handler (processor) is specified, no matter what method the user calls the proxy object, the method will call the invoke method of the processor.

Since the invoke method requires three parameters: proxy object, method and method parameters, no matter which method of the proxy object calls the invoke method of the processor, you must pass in the object, yourself (the method calling the invoke method) and method parameters.

In dynamic proxy technology, no matter what method the user calls the proxy object, it calls the invoke method of the processor written by the developer (this is equivalent to that the invoke method intercepts the method call of the proxy object).

Moreover, through the parameters of the invoke method, developers can also know what method the user calls while intercepting. Therefore, using these two features, they can realize some special requirements, such as intercepting the user's access request to check whether the user has access rights and dynamically add additional functions to an object.

summary

The above is all about Java annotation and dynamic proxy in this paper. I hope it will be helpful to you in Java programming. For more Java content, interested friends can read: things you need to know about java reflection mechanism, building Java projects for ibatis learning, detailed explanation of accommodation examples of Java programming thought objects, etc.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>