Study Spring MVC distributing requests to class instances of spring dependency Injection

Source: Internet
Author: User

Study Spring MVC distributing requests to class instances of spring dependency Injection

The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.



A morning time, touched N nails, constantly pull the nail off, record the choice of tools and methods:

1, first the Spring MVC framework download, then there are three packages are essential:

Spring-webmvc-3.2.9.release.jar

Spring-web-3.2.9.release.jar

Spring-core-3.2.9.release.jar

There is another one, temporarily not considered;

These packages, which were previously downloaded from the Spring official website, call for a fee and can only be downloaded on the Internet for some other people's samples, which are used with packets. In fact, the example of the main, package supplement, from the beginning to learn to do so.

You can search for the desired Jar package from the following address

http://mvnrepository.com/

We enter Spring-webmvc-3.2.9.release.jar Click Search to get the content:


Click the first Spring Web MVC and go to the following page Content section:


Click on the 3.2.9 RELEASE, finally to the target location:


Click the Download (JAR) (623 KB) to download it.

This page, scroll down and find the following section:



In addition to the Java Spec and JSP Tag Library, is the one that comes with the EE, other may have to come down to try, but not all of them are necessary, only when you use the Spring provides this feature, and this feature is based on the specific package listed above, it is required.

It already contains all the jars mentioned above, it's really great maven, but I didn't turn the project into MAVEN management, the old programmer, n years ago, Maven hasn't come out, not quite used to it, but to say it's really good. But the current online commentary, it is a little complex, and Gradle has become a hot, this I believe that the rise of Grails enough to explain.


2, in the process of the configuration from the beginning, according to the example on the Internet, created a dynamic Web project, and added the spring context parameter in Web. XML to specify the spring configuration file location; Add spring's context load listener so that spring is configured, spring There are three kinds of loading methods, in the online read N, from far and near or from near and far, said the Servlet way has not been supported? Plugin mode is not supported for structs 2? Do not know not to confuse, finally, only the listener loading mode is still evergreen, perhaps there is its truth, it is not known, waiting for me to enter the door, and then study.

3, configuration Springmvc dispatcher Servlet and mapping, this, I put the Java Web book ten years ago from the book Heap, a meal turned, and finally familiar with, this is also strange, I remember, when I saw, really can only remember, do not understand, and now is from another angle and direction to see , there is no understanding of the problem, and became a reference book, here to write, where to write, and finally to access this address, to go to the same name of the Servlet is called, highly different, look at the point of view of the problem is really different, of course, in the Java Web facing my height is very low, But I have formed, I am very low, I will put it in the pit to see it, haha, joking ...

With this in place, the default Spring MVC is looking for the corresponding Servlet name that it configures to start with, followed by the-servlet.xml configuration file loading.

Yes, it's here. N a long hou, normal in its configuration file to configure the controller's search package path, then add the annotation @Controller class will be searched, and then according to the methods in these classes before the annotation @RequestMapping(method = Requestmethod.  GET) To find the corresponding mappings, these are not configured in the XML, it is said that this is the reason Spring MVC is now more popular, so that performance without bottlenecks, and Structs team also admit that the way XML configuration files lead to performance problems, these statements, I also hearsay, true and false, at least the latest In the Java Web framework, the search for more Spring MVC is enough to explain the problem.

This request and method of the mapping of the annotated notation, I have to continue in-depth research, testing, and then fill in the instructions.

I simply can enter the breakpoint of this method, and I also have some Mybatis dependency injected in the Spring configuration file, the actual operation, and no interface corresponding to the implementation is injected.

Re-study Spring's dependency injection, only to find that, and N years ago the understanding of a far cry, n years ago, think of it too great, used to be the dynamic detection of the class call, and then to instantiate the object, now understand that the Srping context object has a control reversal container, for storing from the configuration file instantiation of these Bean and assemble them, the so-called assembly, which is whose attribute, then put it in that attribute.

In this way, spring instantiates these classes to form a large string, if there is no container that spring is in charge of, these top class instances no one references it, the result is naturally released, the top-level real, its properties disappear, attribute index class instances are released, the first level down. This is certainly not possible, because Spring does index these class instances and puts them in the context of the WEB application so that they can be accessed.

Really have thought, in the Controller class to take this, but this kind of routine, it is a bit of a look down on spring, based on this idea, spring MVC according to the request to search controller class, according to the note found can respond to the request of the class, how to do?

One, instantiate it, and take the corresponding instance from the spring MVC indexed class, but it is not known how the srping MVC and Spring that are configured in Web. XML are associated, and whether it is actually used or not.

Here is a good idea, in the Spring configuration, the controller class to add a property, and set a string value, in the Controller class, the real addition of a string type of property, so that the breakpoint debugging, enter the controller, found that the value of the new property is not set in the Spring configuration value, This shows that spring MVC does not have those classes that use the spring index, stating that they do not know each other's existence.

On the web, it has been suggested that spring and spring MVC are not available in this way.

So srping MVC does not mentally to this extent, there is no way to integrate with Spring, this one of the things in this situation, it is not the people laughed only blame, it is not a curse in the mess!

There is a need for positive energy, positive energy, people always think in a positive way, that is, these two things can interface with each other, or spring MVC itself has been based on spring is doing things, it will naturally implement the spring mechanism.

Try to put spring's configuration file at the same time as spring MVC, and the net results, both with the parameter name of the same name: Contextconfiglocation, a bit of a meaning, should be feasible.

This means that SPRINGMVC is responsible for the MVC request distribution, but also the management of the class of dependency injection, commissioning, debugging.

Go to the Controller class and look at the values that run the newly added string property, Aha, which is already the attribute value set in the Spring configuration file.


This shows that spring MVC not only handles the request distribution of MVC, but also maintains spring's dependency injection, that is, the instantiation and assembly of classes in the configuration file, when a request is given to the Spring MVC dispatcher, it first specifies the annotations in the path search class, and the method annotations, After finding the class and method that can match the request, locate the class from its maintained Spring dependency Injection class tree and invoke the corresponding method of the class.


At this point, the problem is solved.


Another problem is that Mybatis's mapping of dependency injection, the following official documents in the Java code, how to configure in Spring, a little later research, and then issued:

sqlsession session = Sqlsessionfactory.opensession (); try {  Blogmapper mapper = Session.getmapper (blogmapper.class );  Blog blog = mapper.selectblog (101);} finally {  session.close ();}

----------Supplement----------

The above question, finally found the answer, originally, this need to use mybatis-spring, it will be configured in Spring to inject the available instances;

The following is a way to generate an injection instance for a set of mappers, and the mapperscannerconfigurer is responsible for generating multiple Mapperfactorybean, can be directly injected into the corresponding interface in other places, of course, it is true that the interface declaration with the same package path under the same name. XML mapper file is the premise, otherwise you have to specify the mapper configuration file location, the contract is more than the configuration, Try not to configure, as agreed to come, simple and orderly.



The tangle of these questions stems from the latest official document, which does not explicitly mention dependency injection, and what information is disclosed in an old document? Unknown, or perhaps the latest document, not yet perfect it ...

Mybatis the latest documentation?



Mybatis Old Documents


Oh, this is a mybatis-spring document, the original two are different ... Ah!

Thus, know more and will not use, always better than what do not know to be strong, really want to find information, can also clear, this is the idea of solving the problem!

Usually pull an egg, in fact, is the idea, usually a little more of this aspect of the egg, the real study, it is not so difficult.




Study Spring MVC distributing requests to class instances of spring dependency Injection

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.