Use Java Annotations to manage the life cycle of the object

xiaoxiao2021-04-07  338

Summary

Java Annotations is primarily used to mark the code of deprecated. In this article, they are used to hand over the control of the method call to a lightweight framework to handle components called by a series of methods. Therefore, the correct initialization and settings are delegated to the client application instead of classes to make settings and controls can be adjusted.

For developers, complex applications usually have many initialization problems need to be processed. Many different steps are nothing more than establishing panels, configuring services. The difficulty of these things is that some steps need to be repeated, and others do not need. It is very troublesome to handle this management issue to a class, because logic may change. In addition, modern software design emphasizes separation responsibilities. Simply put, our purpose is to do what to do and how to do it.

This article shows you how to use Annotations to do initial control, which surpasss simple labels. It introduces a small API that can be used to develop your own "Phaseable" Annotations, or give you some inspiration in this new feature.

Copyright Notice: Any website that is authorized by Matrix, please be sure to keep the following authors information and links.

Author: Norbert Ehreke; deafwolf (the author's blog:

http://blog.matrix.org.cn/page/deafwolf)

original:

http://www.matrix.org.cn/resource/Article/44/44403_java annotations.html

Keywords: java; annotations

Annotations

Annotations is a new language feature introduced by J2SE 5.0. Typically, ANNOTATIONS allows developers to mark classes, methods, and members with a secondary information that is independent of running code. This allows an ANNOTATIONS, such as "Good Method", "Bad Method", or more detailed, "non-recommended approach", "overwritten method", "Good Method", "Bad Method", or more detailed, "Good Method", "Writing Method". The possibility of these usage is endless. However, please note that methods or classes are actually not related, such as "not recommended." If you want to know more detailed discussion about Annotations, please read Java 5.0 Tiger: a developer's notebook.

Because Annotations can be used to describe the use of examples or entities such as methods and classes, this is a syntax lollipop. Conversely, these additional information can also be used by other things (such as frameworks) for a wide range of movements, such as generating documents (Javadoc), or as discussed here, as a special content to control behavior, such as the life of the object cycle.

Lifecycle management

Lifecycle management typically happens in an intermediate environment, such as application servers. This idea is to create, use, and destroy the objects of the object. For example, in an application server that publishes different services, it usually does not care about the requested special service (translation: here should be that the application server is a colleague of all requests), the mechanism of calling services is more or less adopted Solution. This depends on the status, caller, and other parameters, some necessary variables, but in an easy-to-manage environment, basic algorithm is usually a series of operational sequence chains. In Java client applications, Mask's display must be processed, or Form allows users to enter or modify data.

Example problem

In Java applications, Mask is usually used for data collection and processing data in the CRUD (Create, Read, Update, delete) cycle. Users can modify, delete, or add some data. As with a simple business problem, we need to manage how to display Mask in client applications. In this way, we separated the display from the operation chain, like this: 1. Creation: Mask is best arranged once in this state.

2. Initialization: In this state, data is retrieved from files and databases, and pops them into Mask fields.

3. Activate: Here, users give up control of Mask.

In reality, involving many aspects: access, verification, control dependence, etc.

Phases

In this discussion, I mentioned the Phase of each step. Basic ideas are very simple: We labeled the class method into the Phases in the operation chain, and then transfer these methods to the service (framework). In fact, this method is not limited to lifecycle management. It can be used as a control mechanism for all calls in the business process.

Our ANNOTATIONS is simple to name PHASE, we use it to mark a method into part of the operation chain. In the following code, you can see the declaration of Annotations is close to the interface.

@ReTENTION (RETENTIONPOLICY.RUNTIME)

@Target ({ElementType.Method})

Public @Interface phase {

Int index ();

String displayName () default "";

Let's take a look at the code. Two lines in the head, you see Annotations for use with the other two Annotations. Just look, this is a bit confusing, but these two lines are very simple to specify AnnotationSphase only allowed and should be retained. The reason why these two Annotations are added because some Annotations may only be used during compilation and may point to classes or members.

@Interface is a standard description of an Annotations. In the next code, INDEX and DISPLAYNAME do not only declare a member, but also declare a method ?? is also a new grammar of Java. If the initial value is not provided, DisplayName will be given an empty string as an initial value, while this DisplayName can be used as a monitoring, called Progress Bar. Index () is must, it tells the framework These Phase can be lacking The execution of the province.

As I have said earlier, we should separate this logic from the object, so we define a interface that must be implemented for call management. This interface can be implemented by a client object. To achieve the purpose of management, we define a universal tag interface, all "Phaseable" interfaces must be inherited here, so the framework can manage classes through a unique access point.

Public interface phased {}

The specific implementation of this interface will look like the code below. Here, the interface defines a MASK, or an Form that includes several operations that must be defined like the above description.

Public interface phasedmask extends phazed {

@Phase (index = 0)

Public void construct ();

@Phase (INDEX = 1)

Public void initialize ();

@Phase (INDEX = 2, DisplayName = "Activating ...") public void activate ();

You can see how to use ANNOTATIONS. It writes the method declaration before and uses an introductory @sign, its property index needs parentheses. Please note that because Annotations is not a Java declaration, no semicolons cannot appear. Now, we need a class to connect these things, and try our Phase that we just defined.

Phaser

The main processing classes may be called Phaser. (Hello, we don't all like Star Travel?) It performs all PHASE and provides users with a simple monitoring mechanism. This type of implementation is not included in this article, of course, you can find the download of the frame code from the resource.

A Phaser has an object that implements some specific PhaseDxxxx interfaces and manages PHASE calls.

Suppose we have a Mymask class like this:

Public class mymask imports phasedmask {

@Phase (index = 0)

Public void construct () {

// do the layout}

@Phase (INDEX = 1)

Public void initialize () {

// Fill the mask with data}

@Phase (INDEX = 2)

Public void activate () {

// Activate the listener and allow the user to interact with the mask}

// Business code}

Now we can call these Phasedmask methods as follows:

Phaser Phaser = New Phaser (Phasedmask); Phaser.invokeAll ();

In this way, the method construct (), initialize (), and activate () are called.

So how do we control PHASE? We skip the construction phase, because when we call PhasedMask () for the second time, it does not need to be arranged again. Orim is that we don't need construct () being called multiple times. Because we use this method with a 0 index annotation, we can simply skip this index and tell Phaser which Phase should do.

Phaser Phaser = New Phaser (PhaseDmask); Phaser.invoke (New Int Int "{1, 2});

This is fine, but it is not clear enough. Who will remember what Phase's index actually represents? Fortunately, we can write more than one below:

Phaser Phaser = New Phaser (Phasedmask);

Phaser.invoke (new string [] {"initialize", "activate"});

Here, we use the method name from the interface inherited. Of course, if needed, we can rearrange the Phase. So, in order to exchange the order, we can write this:

Phaser Phaser = New Phaser (Phasedmask);

Phaser.invoke (New String [] {"Activate", "Initialize"});

This seems to be useful when some Phase is tight when some Phase is tight. Because we are calling methods by reflection here, there are many cases of throwing abnormalities. Phaser captures these exceptions and packages into a so-called PhaseRexception. So, if a method call failed (such as private), Phaser invoke () method throws a PhaseException that contains the original exception. If you don't know much about the reflection, please see "Notes on Reflection" on the side.

You may add a Phaser to a Phaselistener to observe what happened and feeds back to the user during a long Phase call.

Phaselistener Listener = New Phaselistener () {

Public void before (PhaseEvent E) {

// this is caled before the phaser invokes a phase}

Public void after (PhaseEvent E) {

// this is caled after the Phaser Has SuccessFully Invoked a Phase}

}; Phaser Phaser = New Phaser (PhaseDmask); Phaser.addPhaselistener (Listener);

Phaser.invoke (new string [] {"initialize", "activate"});

Discussion and summary

In this article, you see how to use Annotations to manage the survival cycle of the class that is divided into several PHASE. In order to make these classes can be managed by the frame assembly, they must simply implement an interface, which is derived from PhaseD and labeled the method with Phase Annotations. Manage through the Phaser class, which can call the labeling method, and control the order of the call, and provide an event handling mechanism to observe the work of Phaser.

This method also shows a further usage than Javadoc's Annotations. They are not only used for life cycle management, or it can be used for regular object initialization.

Implementation class does not care about the order of their method being called. If you keep this idea in your design, you can use these classes more flexibly.

If Phase must rearrange or ignore, these behaviors will occur in the implementation class.

Like any tool, it has some shortcomings. If the interface must be changed, or the new interface must keep backward compatibility to ensure that the source code is fully available, the implementation class must change. This program lacks support for parameters and returns. The parameters must be provided before the PHASE call. Similarly, because a large number of reflections are used, it will become a bottleneck in a system of high performance requirements.

Finally, the call chain is not clear. In other words, [Please list your favorite javaide], it is impossible to display what method is called when compiling.

About author

Norbert Ehreke is an advanced developer of Impetus Unternehmensberatung GmbH, which is located in Frankfurt in Germany. He is responsible for the development of the framework, and is involved in Perl, Java, and C #. He has studied the Technical University (TU) of Berlin, Germany, Germany's University of Maryland, and Eidgenosche Technische Hochschchchchule (Eth) of Zurich, Switzerland. He received a master's degree in system engineering from Berlin's TU. His interest includes object-oriented programming (Java, Perl, C #), face-oriented programming (AOP), and recent language-oriented programming (LOP). In idle, he likes mountain bikes, cooking and movies. Resource

JavaWorld:

http://www.javaworld.com/

Matrix:

http://www.matrix.org.cn

Download this article source code

Http://www.javaworld.com/javaworld/jw-08-2005/annotations/jw-0801-annotations.zip

Get a Phaser Frame (Sorry, Javadoc is now only a German version. I will localize the document as soon as possible.)

http://www.impetus.biz/downloads.html

Notes about reflection

Java provides the ability to analyze class at runtime. You can get a class that points out its members, methods, and excuses it implemented. You can also find these parameters, that is, a method of parameter type, return type, and the name of the method and member. And, you can access the members you found, that is, you can call methods, operate public members, and more.

This is a very powerful ability, because we can handle the class we have not introduced when we have compiled. This Java feature is the foundation of the framework that I discussed in this article.

转载请注明原文地址:https://www.9cbs.com/read-132397.html

New Post(0)