PrefaceAbout the SPRINGMVC initialization contextloader in the Xmlwebapplicationcontext, as well as dispatcherservlet initialization and so on, such a principle has been more than N predecessors and cattle people summed up, I'm not going to repeat the wheel here. ~
BodyFinding the Dodispatch body in the Dispatcherservlet class, we can see that it is equivalent to calling the Doservice in the servlet. This is used to pass the request to the controller we wrote and execute the appropriate method, returning the Modeview object.
Code snippet to execute:
- ...
- Handleradapter ha = Gethandleradapter (Mappedhandler.gethandler ());
- MV = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ());
- ...
... Handleradapter ha = Gethandleradapter (Mappedhandler.gethandler ()); mv = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ()); ....
Mappedhandler.gethandler () is the controller object and is not used directly Invokes a custom method written in the. Handlerrequest or Multiactioncontroller, and takes a Handleradapter interface.
Adapter mode is used here, because the type of controller is different, there are multiple implementations, then the invocation method is not deterministic, if you need to call the Controller method directly, you need to write the following form in the code: Java code
- if (Mappedhandler.gethandler () instanceof Multiactioncontroller) {
- ((Multiactioncontroller) Mappedhandler.gethandler ()). xxx
- }Else if (Mappedhandler.gethandler () instanceof XXX) {
- ...
- }Else if (...) {
- ...
- }
- ...
if (Mappedhandler.gethandler () instanceof Multiactioncontroller) { (Multiactioncontroller) Mappedhandler.gethandler ()). Xxx}else if (Mappedhandler.gethandler () instanceof xxx) { ...} else if (...) { ...} ...
This assumes that if we add a hardcontroller, we have to add a line of if (Mappedhandler.gethandler () instanceof Hardcontroller) to the code, which makes the program difficult to maintain. Also violates the principle of opening and closing in design mode-open for expansion, close for modification.
Therefore, spring defines an adaptation interface so that each controller has a corresponding adapter implementation class that allows the adapter to perform the appropriate method instead of the controller. So in the expansion controller, only need to add an adapter class to complete the expansion of the SPRINGMVC, it is really a very ingenious way!
Nonsense not much to say or on the code bar, in order to see clearly, the realization of a set of code to simulate SPRINGMVC, directly paste Spring source code easy to reduce the focus. Java code
- Define a adapter interface
- Public interface Handleradapter {
- Public Boolean supports (Object handler);
- public void handle (Object handler);
- }
- Here are three controller implementations
- Public interface Controller {
- }
- Public class Httpcontroller implements controller{
- public void Dohttphandler () {
- System.out.println ("http ...");
- }
- }
- Public class Simplecontroller implements controller{
- public void Dosimplerhandler () {
- System.out.println ("simple ...");
- }
- }
- Public class Annotationcontroller implements controller{
- public void Doannotationhandler () {
- System.out.println ("annotation ...");
- }
- }
- Write the adapter class below
- Public class Simplehandleradapter implements Handleradapter {
- public void handle (Object handler) {
- ((Simplecontroller) handler). Dosimplerhandler ();
- }
- Public Boolean supports (Object handler) {
- return (handler instanceof Simplecontroller);
- }
- }
- Public class Httphandleradapter implements Handleradapter {
- public void handle (Object handler) {
- ((Httpcontroller) handler). Dohttphandler ();
- }
- Public Boolean supports (Object handler) {
- return (handler instanceof Httpcontroller);
- }
- }
- Public class Annotationhandleradapter implements Handleradapter {
- public void handle (Object handler) {
- ((Annotationcontroller) handler). Doannotationhandler ();
- }
- Public Boolean supports (Object handler) {
- return (handler instanceof Annotationcontroller);
- }
- }
- Simulate a Dispatcherservlet
- Import java.util.ArrayList;
- Import java.util.List;
- Public class Dispatchservlet {
- public static listnew arraylist
- Public Dispatchservlet () {
- Handleradapters.add (new Annotationhandleradapter ());
- Handleradapters.add (new Httphandleradapter ());
- Handleradapters.add (new Simplehandleradapter ());
- }
- public void Dodispatch () {
- //Here simulates SPRINGMVC from request to fetch handler object, just new out, can out,
- //No matter what controller is implemented, the adapter will always be able to get the desired result after fitting
- Httpcontroller controller = new Httpcontroller ();
- Annotationcontroller controller = new Annotationcontroller ();
- Simplecontroller controller = new Simplecontroller ();
- //Get the corresponding adapter
- Handleradapter adapter = gethandler (Controller);
- //The corresponding controller corresponding method is executed through the adapter
- Adapter.handle (Controller);
- }
- Public Handleradapter GetHandler (Controller controller) {
- For (Handleradapter adapter: this.handleradapters) {
- if (adapter.supports (Controller)) {
- return adapter;
- }
- }
- return null;
- }
- public static void Main (string[] args) {
- new Dispatchservlet (). Dodispatch ();
- }
Application of SPRINGMVC Adapter