Author: Jiangnan Baiyi
This article is from springside wiki. Pay attention to the latest version in wiki.
1. Interface
Interface is used to define the object type. The framework provides services based on the interface of the object. This mode is a must-learn Method for Java framework designers, from the most weighted EJB to the most lightweight spring, this method is inseparable and indeed solves many problems. For example, in spring that everyone is familiar:
- Beanfactoryaware interface. The framework will call the setbeanfactory (beanfactory) function of bean and pass beanfactory to it so that it can use beanfactory to obtain more beans, instead of relying on injected beans.
- Factorybean interface, the Framework does not put this bean into its own context, but calls its GetObject () function to put the returned results as beans. Many functions in spring depend on this extension mechanism because it can return any type of objects in factory mode, rather than the class specified type in <Bean class = "foo">.
- Initializingbean interface, because many beans need a total init function to initialize after each setter is injected. For example, the value of attribute C needs to be initialized Based on the injected attributes A and B at the same time. In this case, the framework will call afterpropertiesset () for initialization.
Wait. The last initializingbean interface is evil !! Pojo is required to implement a strange afterpropertiesset () function.
2. No intrusion
Therefore, Spring provides another way to define the initial method name in the configuration file: <Bean class = "foo" init-method = "init"/>
In fact, spring advocates two non-intrusive levels:
One is for modules such as quartz, JMX, and WebService. pojo is useless. Spring uses mechanisms such as AOP and factorybean to enable them to perform WebService on a regular basis.
One is like the init-method example. pojo does not obviously implement the spring interface. It perceives the existence of spring and implements its own functions, then they are called by the spring configuration file.
The previous non-intrusive method has a very obvious effect. The significance of the latter method depends on the specific scenario. Sometimes there is nothing to say about implementing the spring interface, however, you may want to implement the interface explicitly for various reasons.
3. Annotation
The annotation mode is added in jdk5. The annotation mode is a competitor of the XML mode, but the question is too big. I will not discuss it in this article. My point is mainly to see whether the data is configuration data or code metadata.
In this document, forcuinterface vs annotation is used to define the object type and behavior.
At first, the use of annotation made Java code no longer so limited by the traditional mode testng. Then JUnit 4 followed closely. Besides the default mechanism, annotation can be used to flexibly define setup and test functions.
4. Comparison and demonstration
The following describes how to implement the undeleteentity interface and @ undelete identifier in springside.
If the entity object is defined as an object that cannot be deleted, the remove function of the base class does not actually Delete the object, but sets the State column as "invalid ".
The interface is implemented as follows:
Interface Definition:
Public interface undeletableentity {
Void setstatus (string status );
}
Interface Used in entity:
Public class Book implements undeletableentity {
Private string status; Public void setstatus (string status ){
This. Status = status;
}
}
Interface Used in the framework:
// Judge based on class
If (undeletableentity. Class. isassignablefrom (entityclass )){
...
}
// Judge based on bean entity
If (bean instanceof undeletableentity {
(Uldeletableentity) bean). setstatus ("-1 ");
}
If everyone is familiar with the writing method, I will not explain it.
Annotation is implemented as follows:
Annotation definition:
@ Target ({elementtype. Type })
@ Retention (retentionpolicy. runtime)
Public @ interface undeletable {
String status () Default "status ";
}
Originally, Sun was so stingy that @ interface was used instead of another keyword. Sun originally meant to locate the interface and annotation.
@ Target defines where annotation can be used, such as types, functions, and function parameters. Type indicates class, interface...
@ Retention (retentionpolicy. runtime) indicates a runtime annotation. Otherwise, the subsequent code will not work.
String status () defines that annotation has a status attribute, which can be left unspecified. The default value is status.
Annotation is used in entity:
@ Undeletable
Public class book {
Private string status;
Public void setstatus (string status ){
This. Status = status;
}
}
@ Undeletable (status = "status2 ")
Public class bookwithnewstatus {
Private string status2;
Public void setstatus2 (string status2 ){
This. status2 = status2;
}
}
Use of annotation in the framework:
If (entityclass. isannotationpresent (undeletable. Class )){
Undeletable Anno = (undeletable) entityclass. getannotation (undeletable. Class );
Statusproperty = Anno. Status ();
}
It can be seen that the annotation mode is less invasive than the interface, and the name of the Status column is not fixed, and more attributes can be flexibly defined, for example, if the definition is invalid, the value is "-1" and "unvalid ".
However, this mode is the same as all the dynamic East-West modes and loses the advantage of the validation during compilation. If the pojo function does not have setstatus (), it cannot be checked during compilation.
BTW. In fact, this article also belongs to the kindergarten qionglin series. Show the basic method of defining metadata with interface and annotation and hand it over to the framework for reflection.