I. Dynamic Agent 1.1, Proxy mode what is proxy mode and its function
Proxy pattern (i.e., agent mode), one of the 23 common object-oriented software design patterns
Proxy mode definition: Provides a proxy for other objects to control access to this object. In some cases, an object does not fit or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object.
Advantages:
- (1). The role of clear and real responsibility is to achieve the actual business logic, do not care about other non-responsibility transactions, through the late agent to complete a complete transaction, the accompanying result is simple and clear programming.
- (2). The proxy object can act as an intermediary between the client and the target object, which plays a role in hiding and protecting the target object.
- (3). High scalability
Structure
- One is the real object you want to access (the target class), the other is the proxy object, the real object and the proxy
- Object implements the same interface, first accesses the proxy class and then accesses the object that is actually being accessed.
1.2. Dynamic Agent
Dynamic proxy It can generate a proxy object directly to a target object without the need for a proxy class to exist.
Dynamic agent and Proxy mode principle is the same, but it does not have a specific proxy class, directly through the reflection generated a proxy object.
Dynamic Agent Generation Technology:
- The JDK provides a proxy class that can directly generate agent objects directly to the object implementing the interface class.
- Cglib (Spring Learning)
The Java.lang.reflect.Proxy class can generate a proxy object directly
Proxy.newproxyinstance (): Generates an instance of the proxy class. A class that can only be proxied to implement at least one interface
- ClassLoader: Class loader. Fixed notation, and the same class loader used by the proxy class.
- Class[] Interface: The interface to be implemented by the proxy class. Fixed notation, and the same interface can be used by the proxy class.
- Invocationhandler: Application of the Policy (scheme) design pattern . How to proxy?
- Invoke method in Nvocationhandler: Any method that invokes the proxy class, this method executes the
- Object Proxy: A reference to the proxy object itself. Generally not.
- Methods: The method that is currently being called.
- object[] args: Parameters used by the current method
1.3, AOP programming ideas (slicing programming)
1. Problem: There will be a lot of business methods in the future, there will be many duplicate code
2. Question: There are already many methods that do not take into account the problems of the business, and now ask to add.
Second, note:
- Note It is not a comment comment is written by the programmer, to the programmer's
- Annotations are given to the program to describe how the program runs and at what stage.
Annotations now in real-world development, the biggest feature is to replace the configuration file. Annotations are a new feature of jdk1.5 and can be reflected to make annotations functional.
Annotation @xxxx
2.1. Custom Annotations 1, three basic annotations in JDK:
A, @Override: The check subclass is indeed a method that overrides the parent class.
B, @Deprecated: The explanation is out of date.
C, @SuppressWarnings ({"Unused", "deprecation"}): Suppresses warnings in the program. The type of unused warning. {} array. All suppresses all warnings.
@SuppressWarnings ("All")
2. Syntax for custom annotations: (physical)
Look at the nature of annotations
Declares an annotation @interface note name {}
Public @interface myannotation{}
Note Its essence is an interface, which needs to inherit the annotation interface.
Public interface Myannotation extends Java.lang.annotation.Annotation {
}
Analyze members in annotations
Annotations are essentially interfaces and can have attribute methods in interfaces
Properties: Example: int age ();
What are the attribute types for annotations?
- Basic type
- String
- Enum type
- Annotation type
- Class type
- One-dimensional array type of the above types
Note: A tag is added to a location in your program code.
3. Reflection of Annotations: (Soul)
A, reflective annotation class
Java.lang.reflect.AnnotatedElement:
- <t extends annotation> T getannotation (class<t> annotationtype): Gets a note reference of the specified type. No null returned.
- Annotation[] Getannotations (): Gets all the annotations, including inherited from the parent class.
- Annotation[] Getdeclaredannotations (): Get annotations on yourself.
- Boolean isannotationpresent (class<? extends Annotation> Annotationtype): Determines if the specified annotation is not.
Class, Method, Field, constructor, etc. implement the Annotatedelement interface.
such as: Class.isannotationpresent (Mytest.class): Judgment class above there is no @mytest annotation;
Method.isannotationpresent (Mytest.class): There is no @mytest annotation on the judging method.
B. Properties in reflection annotations
4, Yuan annotation
A, custom annotations survival range (life cycle): Default is class.
What are meta annotations:
Annotations that can only be used on annotations are called meta annotations. (that is, annotations used to modify annotations)
@Retention: role. Changes the survival scope of the custom annotations.
- Retentionpolicy:
- SOURCE
- CLASS
- RUNTIME
@Target: The function that specifies where the annotation can be used.
- ElementType:
- TYPE:
- METHOD:
- FIELD:
- Annotation_type
@Documented: The function, the class using the @mytest annotation, if the @mytest annotations have @documented annotations above, then the @mytest annotation of the class's API document will appear @mytest figure.
@Inherited: A function that indicates that the annotation can be inherited.
Brief introduction of several annotations in Servlet3.0
Added support for annotations.
Servlet3.0:
tomcat7+;jdk6.0+;
Servlet3.0:web.xml is not a must. the alternative is annotations.
Four, class loader
1, function: Responsible for loading the class file on the disk into the JVM, class reference byte code
2. The class loader in the JVM:
- BootStrap: Boss. The ancestor of the class loader. Printing it will get null.
- Responsible for loading Jre/lib/rt.jar (most of the classes in the JDK)
- Extclassloader: Responsible for loading Jre/lib/ext/*.jar
- Appclassloader: All classes that are responsible for loading in the CLASSPATH environment variable.
3. Parent class delegation mechanism
Class A extends HttpServlet find A and then look up (the classes you write will be placed under the classpath path)
Agent mode of Javaweb