Agent mode of Javaweb

Source: Internet
Author: User

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:

    1. The JDK provides a proxy class that can directly generate agent objects directly to the object implementing the interface class.
    2. 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?

    1. Basic type
    2. String
    3. Enum type
    4. Annotation type
    5. Class type
    6. 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:

    1. <t extends annotation> T getannotation (class<t> annotationtype): Gets a note reference of the specified type. No null returned.
    2. Annotation[] Getannotations (): Gets all the annotations, including inherited from the parent class.
    3. Annotation[] Getdeclaredannotations (): Get annotations on yourself.
    4. 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:

    1. 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)
    2. Extclassloader: Responsible for loading Jre/lib/ext/*.jar
    3. 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

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.