Application of SPRINGMVC Adapter

Source: Internet
Author: User

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:
    1. ...
    2. Handleradapter ha = Gethandleradapter (Mappedhandler.gethandler ());
    3. MV = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ());
    4. ...
... 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
    1. if (Mappedhandler.gethandler () instanceof Multiactioncontroller) {
    2. ((Multiactioncontroller) Mappedhandler.gethandler ()). xxx
    3. }Else if (Mappedhandler.gethandler () instanceof XXX) {
    4. ...
    5. }Else if (...) {  
    6. ...
    7. }
    8. ...
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
  1. Define a adapter interface
  2. Public interface Handleradapter {
  3. Public Boolean supports (Object handler);
  4. public void handle (Object handler);
  5. }
  6. Here are three controller implementations
  7. Public interface Controller {
  8. }
  9. Public class Httpcontroller implements controller{
  10. public void Dohttphandler () {
  11. System.out.println ("http ...");
  12. }
  13. }
  14. Public class Simplecontroller implements controller{
  15. public void Dosimplerhandler () {
  16. System.out.println ("simple ...");
  17. }
  18. }
  19. Public class Annotationcontroller implements controller{
  20. public void Doannotationhandler () {
  21. System.out.println ("annotation ...");
  22. }
  23. }
  24. Write the adapter class below
  25. Public class Simplehandleradapter implements Handleradapter {
  26. public void handle (Object handler) {
  27. ((Simplecontroller) handler). Dosimplerhandler ();
  28. }
  29. Public Boolean supports (Object handler) {
  30. return (handler instanceof Simplecontroller);
  31. }
  32. }
  33. Public class Httphandleradapter implements Handleradapter {
  34. public void handle (Object handler) {
  35. ((Httpcontroller) handler). Dohttphandler ();
  36. }
  37. Public Boolean supports (Object handler) {
  38. return (handler instanceof Httpcontroller);
  39. }
  40. }
  41. Public class Annotationhandleradapter implements Handleradapter {
  42. public void handle (Object handler) {
  43. ((Annotationcontroller) handler). Doannotationhandler ();
  44. }
  45. Public Boolean supports (Object handler) {
  46. return (handler instanceof Annotationcontroller);
  47. }
  48. }
  49. Simulate a Dispatcherservlet
  50. Import java.util.ArrayList;
  51. Import java.util.List;
  52. Public class Dispatchservlet {
  53. public static listnew arraylist
  54. Public Dispatchservlet () {
  55. Handleradapters.add (new Annotationhandleradapter ());
  56. Handleradapters.add (new Httphandleradapter ());
  57. Handleradapters.add (new Simplehandleradapter ());
  58. }
  59. public void Dodispatch () {
  60. //Here simulates SPRINGMVC from request to fetch handler object, just new out, can out,
  61. //No matter what controller is implemented, the adapter will always be able to get the desired result after fitting
  62. Httpcontroller controller = new Httpcontroller ();
  63. Annotationcontroller controller = new Annotationcontroller ();
  64. Simplecontroller controller = new Simplecontroller ();
  65. //Get the corresponding adapter
  66. Handleradapter adapter = gethandler (Controller);
  67. //The corresponding controller corresponding method is executed through the adapter
  68. Adapter.handle (Controller);
  69. }
  70. Public Handleradapter GetHandler (Controller controller) {
  71. For (Handleradapter adapter: this.handleradapters) {
  72. if (adapter.supports (Controller)) {
  73. return adapter;
  74. }
  75. }
  76. return null;
  77. }
  78. public static void Main (string[] args) {
  79. new Dispatchservlet (). Dodispatch ();
  80. }

Application of SPRINGMVC Adapter

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.