Architecture exploration-building a framework and building a framework

Source: Internet
Author: User

Architecture exploration-building a framework and building a framework

Build a lightweight Java Web Framework
Quickly build a development framework
How to load configuration files
How to implement a simple IOC container
How to load the specified class
How to initialize the framework

* Annotation Development

Objectives:
To build a lightweight MVC Framework, Controller is the core of MVC, similar to SpringMVC.
The Controller annotation is used to define the Controller class. In this class, a series of Service member variables can be defined through the Inject annotation, which is "dependency injection ".
In addition, there are a series of Action methods defined by the Action annotation. In these Action methods, the Service member variable method is called to complete the specific business logic.
If the View object is returned, it indicates the JSP page. If the Data object is returned, it indicates a JSON Data.

Simply put, a Controller class contains multiple Action methods, which can return View or Data objects, corresponding to JSP pages or JSON Data respectively.


@ Controller Annotation

@ Service Annotation

@ Action Annotation

@ Inject Annotation


View class: View path and model data

Data: model Data

Param class: encapsulate objects with Request Parameters

Request object: encapsulate the Request (Request Method and Request Path)

Handler object: encapsulate Action information (Controller class and processing method)


Core: DispatchServlet: the Helper class is initialized when the init method is called. Service method to obtain and process requests

PropUtil → ConfigHelper: load the configuration item and obtain the content of the configuration item definition.
ClassUtil → ClassHelper: obtains all classes, Controller classes, Service classes, and Bean classes under the specified package. (CLASS_SET container concept)
ReflectionUtil → BeanHelper: Creates a Bean container and stores the ing between Bean classes and Bean instances in BeanMap. (BEAN_MAP is a container)
IocHelper: dependency Injection
ControllerHelper: Relationship between request processing and request processing methods (ACTION_MAP is also a container ).
HelperLoader: Four Helper classes are loaded by an entry program, which is actually loading their static blocks.


Basics:
Define framework configuration items:
Jdbc-related configuration, project basic package name, JSP basic path, and static resource file basic path
Provides related configuration item Constants

Load the configuration item and obtain the content defined by the configuration item: supported by PropUtil (loading the configuration file from the current thread) → ConfigHelper


Development Loader:
Develop a class loader to load all the classes under the basic package, such as classes with a certain annotation or classes that implement a certain interface, or inherit all subclasses of a parent class.

ClassUtil tool class: provides methods related to class operations,
For example
Get ClassLoader: you only need to obtain the ClassLoader in the current thread.
Loading Class: public static Class <?> ForName (String className, boolean initialize, ClassLoader loader ). Initialize indicates whether to Load Static code blocks.
Obtains all classes under a specified package.

Obtain all classes under the application package, all Service classes under the application package, and all Controller classes under the application package.
Obtain the class with Controller and Service (Bean class, managed by IOC container) → ClassHelper


Implement the Bean container (the container creates an object Using Reflection and ClassHelper)

ReflectionUtil (reflection tool class): creates an instance, calls a method, and sets the value of a member variable.
BeanHelper: gets the Bean class managed by the framework, and then creates an instance through the reflection tool class. Put the object created each time into a static Map <Class <?>, Object> class.

Implement the dependency injection function (instantiate the member variables marked with an annotation through the container ):
Use the Inject annotation to instantiate the Service.
Specific implementation: Get all BeanMap structures through BeanHelper, and record the ing relationship between classes and objects. Then, traverse the ing relationship and retrieve the Bean class and Bean instance respectively,
Then, all member variables in the class are obtained through reflection. Continue to traverse these member variables to see if these member variables contain the Inject annotation.
BeanMap extracts Bean instances based on Bean classes. Finally, use the ReflectionUtil # setField method to modify the value of the current member variable.

Load Controller:
Through ClassHelper, We can get all classes that define Controller annotations, and get all the methods with Action annotations in the class ("Action" for short) through reflection,
Obtain the Request expression in the Action annotation, then obtain the Request Method and Request Path, encapsulate a Request object (Request) and a processing object (handler), and finally set the Request and Handler
Create a ing relationship, put it in an ActionMap, and provide a method to obtain the processing object based on the Request Method and Request Path.

ControllerHelper: encapsulates an ActionMap, stores the mappings between requests and Handler, and obtains all classes with Controller annotations through ClassHelper,
Traverse these Controller classes, extract the URL from the Action annotation, and initialize the Request ing between the Request and Handler.

Core:
Request forwarder (processing requests and responses ):
Process all requests and obtain the Request Method and path from the HttpServletRequest object. Use the ControllerHelper # getHandler method to obtain the Handler object.
After obtaining the Handler object, we can easily obtain the Controller class and get the Controller instance object through the BeanHelper. getBean method.
Then, you can obtain all the request parameters from the HttpServletRequest object and initialize them to a Param object.
You can also obtain the return value of the Action method from the Handler object. The return value may be in two situations:
(1) If the returned value is a View object of the View type, a JSP page is returned.
(2) If the returned value is a Data object of the Data type, a JSON Data is returned.

Summary:
The Controller class is defined through the Controller annotation, dependency injection is implemented through the Inject annotation, and Action method is defined through the Action annotation.
Initialize the MVC framework through a series of Helper classes. The DispatchServlet is used to process all requests, and the specific Action method is mobilized Based on the Request Method and Request Path to determine
The return value of the Action method. If it is View type, it is displayed on the JSP page. If it is Data type, JSON Data is returned.

 

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.