The principle of lightweight Java Web framework

Build a lightweight Java Web framework

    MVC (Model-View-Controller, Model - View - Controller) is a common design pattern, this pattern may be used to decouple the application.

IOC

    Controller Controller class is defined by annotations in the class, member variables can be defined by a series of Service Inject annotation, which is "dependency injection." In addition, there are a series of methods (referred to as the Action method) is defined by the Action notes in these Action method, call the Service member variables method to accomplish specific business logic. View objects if returned, indicates JSP page; if the Data object returns a JSON data indicates.

    We need to develop a "class loader" to load all classes in the base package name, such as the use of certain annotated classes or class that implements an interface, and then inherit all or subclasses such as a parent class.

    Get the class loader to implement the most simple, just to get ClassLoader of the current thread.

    Our goal is to use annotation on the controller class Controller, using the Action annotation on the controller class method, use Service annotation on the service class, use the Inject annotation in the controller class will come in the service class dependency injection. 4 it is necessary to customize annotation category.

    Controller objects can be annotated with the annotation Service class generated by the understanding of the management framework Smart Bean.

    We need to get all categories Bean Smart management framework, according to the class to instantiate an object, the object is created each time and finally stored in a static Map <Class <?>, Object>, we need to get the Map at any time, but also We need to acquire value (Bean object) corresponding to the Map by the key (class name). The Map <Class <?>, Object> is equivalent to a "Bean container" to store the mapping between classes and Bean Bean in Bean Map instance, we only need to getBean method, passing in a class Bean, Bean will be able to get instance.

    We then defined Service Cotroller member variable, then call the Service member variables in the Action Controller's method of approach. So, how to instantiate the Service member variables?

    Remember Inject annotation previously defined it? We use it to implement Service instantiated. So, who is going to instantiate it?

    Not new developer itself by way of example, but rather be instantiated frame itself, such as the instantiation process, called IoC (Inversion of Control, inversion control). Control is not determined by the developer, but to reverse the frame. In general, we will also be referred to as Inversion of Control DI (Dependency Injection, dependency injection), can be understood as a class will need to rely on members injected into this category. So, how do dependency injection?

    The easiest way is to first get all BeanMap by BeanHelper (a Map <Class <?>, Object> structure, records mapping between classes and objects). Then traverse the mapping relationship, and are removed Bean Bean class instance, then obtain all class member variables by reflection. The member variables continue to traverse, determines whether the loop variable with the current member Inject annotations, if associated with the annotation, is taken out from the Map Bean Bean Bean example of a class. Finally, to modify the current value of a member variable by ReflectionUtil # setField method.

    In this case, the IoC framework objects are managed by a single case, because the IoC framework underlying or obtain Bean Map from BeanHelper rather Bean Map of the objects are created in advance and put into the Bean container, All objects are of simple interest.

Request Forwarder

    We need to create a ControllerHelper class, it follows logic to handle:

    By ControllerHelp, we have access to all classes defined Controller annotation, all such methods may be obtained by reflecting with Action annotated (referred to as "Action" method), the expression Action annotation acquisition request, and then requests the acquisition request method path, encapsulating a request object (request) and processed (Handler), and finally with the request Handler to establish a mapping relationship, placed in an Action map, and provides a method of obtaining a processed request method according to the request path.

    Now you need to write a Servlet, let it handle all requests. The method of obtaining the request from the request path HttpServletRequest object, acquired by ControllerHelper # getHandler Handler object method. When get Handler object, we can easily obtain Controller class, and further obtain the object instance Controller by BeanHelp.getBean method.

    A simple frame to completion of the development of MVC, through this DispatcherServlet to handle all requests, obtain from the corresponding Action ControllerHelper method according to the request information, and then using the reflection technique calls Action method while requiring specific method parameters passed, and finally take and determines the type of the return value of the return value, performs corresponding processing.

    Controller Controller annotation defined by class; dependency injection is achieved by Inejct annotation; method defined Action Action annotation. Through a series of Helper class to initialize the MVC framework. To process all requests by DispatcherServlet; Action methods to mobilize specific request method according to the path request, the return value is determined Action, if the type of View, jump to a JSP page, if the Data type, returned JSON data.

AOP

    In AOP, we need to define a Aspect (section) class to write code requires cross-business logic, that is, performance monitoring codes mentioned above. In addition, we need to match the class want to block by a condition, the condition is called Pointcut (cut-off point) in the AOP.

    Agent, or called Proxy, meaning that you do not do it, someone else instead of you to deal with.

    Acting through dynamic CGLib

    In the client code inherited AspectProxy;

    by proxy for the target class CGLib returned after having achieved AOP, targetClass original class

    Object proxy = ProxyManager.createProxy(targetClass, proxyList);
BeanHelper.setBean(targetClass, proxy);

Guess you like

Origin www.cnblogs.com/kexinxin/p/11608079.html