Spring MVC stands straight and says, "dependency injection, I am here !, Mvc waist

Source: Internet
Author: User

Spring MVC stands straight and says, "dependency injection, I am here !, Mvc waist

Study Spring MVC to distribute requests to Spring dependent injection class instances

Beautiful Life of the sun and fire god (http://blog.csdn.net/opengl_es)

This article follows the "signature-non-commercial use-consistency" creation public agreement

Reprinted please keep this sentence: Sun huoshen's beautiful life-this blog focuses on Agile development and mobile and IOT device research: iOS, Android, Html5, Arduino, pcDuino, otherwise, this blog post is rejected or reprinted. Thank you for your cooperation.



In the morning, I touched N nails and continuously dropped the nails. I recorded the tools and methods of the selection:

1. First download the Spring Mvc framework, it is essential to have three packages:

Spring-webmvc-3.2.9.RELEASE.jar

Spring-web-3.2.9.RELEASE.jar

Spring-core-3.2.9.RELEASE.jar

Another one is not considered;

These packages were previously downloaded from the Spring official website, which is a tough task. You can only download some examples of others from the Internet. In fact, it is mainly an example, supplemented by a package. This is the case from the beginning.

Find the desired Jar package from the address below

Http://mvnrepository.com/

We enter spring-webmvc-3.2.9.RELEASE.jar points and search to get the content:


Click the first Spring Web MVC and transfer it to the following page:


Click 3.2.9 RELEASE to reach the target location:


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

Scroll down and find the following content:



Except Java Spec and JSP Tag Library, they are libraries that come with J2EE. Other libraries may have to be tested. However, not all libraries are required, this feature is only required when you use the feature provided by Spring, which relies on the specific packages listed above.

These include all the Jar files mentioned above. It's really amazing Maven, But I didn't translate the project into Maven management. Old programmers used it N years ago, maven hasn't come out yet, so I'm not used to it, but it is really good to say it is. However, the current online comments are a bit complicated, and Gradle becomes a hot topic. I believe that the rise of Grails is enough to explain.


2. A dynamic Web project is created based on the online example during the configuration process from scratch. spring context parameters are added to xml to specify the location of the Spring configuration file; Spring context loading listener is added, so that Spring is configured completely. Spring has three loading methods, I have read N Articles on the Internet. from far and near or near to far, I have said that the Servlet method is no longer supported? Is the plug-in method not supported for structs 2? I don't know how to mix it up. In the end, only the Loading Method of the listener is still green. Maybe there is a reason for it. I don't know. I will study it again when I open the door.

3. Configure SpringMVC's distributor Servlet and ing. In this case, I pulled the Java Web book from the library a decade ago and finally got familiar with it, this is also strange. I still remember it. When I saw it, I could only remember it, but I really couldn't understand it. Now, from another perspective and direction, there is no problem I don't understand, I have become a reference book. Here I want to write it like this, where I want to write it like that, and finally assign the access address to the Servlet with the same name to be called. The height is different, the angle of viewing the problem is indeed different. Of course, the Java Web is very low, but I have developed it. When it is very low, I will put it in the pit to see it. Haha, joke...

After configuration, Spring MVC finds the corresponding Servlet name for its configuration by default, and loads it with the configuration file-servlet. xml.

Yes, that is, if I have struggled with N for many times and configured the search package path of the Controller in the configuration file, the class with the annotation @ Controller will be searched, then, according to the annotation @ RequestMapping (method = RequestMethod. GET) to find the corresponding ing, these do not need to be configured in XML, it is said that this is a popular reason for Spring MVC, so there is no performance bottleneck, the Structs team also admitted that the XML configuration file method has caused performance problems. These statements are also heard and hard to argue about. At least for the latest Java Web framework, spring MVC is enough to illustrate the problem.

This request and method ing annotation writing, I have to continue in-depth research, testing, and then supplement the description.

I can simply break the breakpoint of this method, and I also configured some Mybatis dependencies to be injected into the Spring configuration file. In actual operation, no implementation corresponding to the interface is injected.

After studying Spring's dependency injection again, we found that it was far from understanding it N years ago. N years ago, we thought about it too well. In the past, it was a call of the dynamic detection class, then I instantiate the object. Now I understand that there is a control reversal container in the Srping context object, which is used to store the beans instantiated from the configuration file and assemble them. The so-called assembly, that is, the attribute of who is put in that attribute.

In this case, the classes instantiated by Spring form a large string. Without the containers managed by Spring, no one will reference these class instances at the top level, and the result will naturally be released, when the top-level instance is put, its attributes disappear, and the class instance of the attribute index is released, the first-level instance goes down. This is of course impossible, because Spring indeed indexes these class instances and puts them in the Web application context so that they can be accessed.

I did think about using this method in the Controller class, but it's easy to look at Spring. Based on this idea, Spring MVC searches for the Controller class based on requests, what should I do after I find the class that can respond to this request by annotation?

One is to instantiate it, and the other is to get the corresponding instance from the Spring MVC index class, but you do not know the web. how to associate Srping MVC and Spring configured in xml, but whether or not they are used is unknown.

Here comes a good idea. In Spring configuration, add an attribute to the Controller class and set a string value. In the Controller class, add a character string attribute, in this way, the breakpoint debugging will go to the Controller and find that the value of the newly added attribute is not the value set in Spring configuration, spring MVC does not use the classes indexed by Spring, which means they do not know each other's existence.

According to the network query, some people have suggested that Spring and Spring MVC are not available together.

So Srping MVC won't be mentally retarded to this level, so it cannot be integrated with Spring. This kind of thing will not be laughed at by others, isn't that a curse!

At this time, we need positive energy. In front of positive energy, people will always think about the problem in a positive way. That is, these two things can either interface with each other, either Spring MVC itself is already doing things based on Spring, and it will naturally implement the Spring mechanism internally.

Try it. Assign the Spring configuration file to Spring MVC at the same time, and check the results on the net. The two use the same name parameter name: contextConfigLocation, which is a bit interesting and should be feasible.

SpringMVC is responsible for the distribution of MVC requests, and management of dependency injection, start, and debugging.

Go to the Controller class and check the value of the new string attribute added during the runtime. Haha, the attribute value is already set in the Spring configuration file.


It can be seen that Spring MVC does not only process MVC request distribution, but also maintains Spring dependency injection, that is, class instantiation and assembly in the configuration file, when a request is sent to the Spring MVC distributor, it first specifies the annotation in the path Search Class and method annotation, and finds the class and method that can match the request, find the class from the Spring dependency injection class tree it maintains and call the corresponding method of the class.


So far, the problem has been solved.


Another problem is the dependency injection of Mybatis ing. The following is the Java code in the official documentation. how to configure it in Spring:

SqlSession session = sqlSessionFactory.openSession();try {  BlogMapper mapper = session.getMapper(BlogMapper.class);  Blog blog = mapper.selectBlog(101);} finally {  session.close();}

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

The above question is finally answered. It turns out that MyBatis-Spring is used to inject available instances through Spring;

The following is a method for generating injection instances for a set of mappers. MapperScannerConfigurer is responsible for generating multiple mapperfactorybeans, which can be directly injected to the corresponding interfaces in other places. Of course, indeed, the interface declaration has the same name under the same package path. the xml er file is a prerequisite. Otherwise, you have to specify the location of the er configuration file. The conventions are more important than configurations. Try not to configure them. Just follow the conventions, which are simple and organized.

(Here we need to add a note that although MapperFactoryBean seems to be injected, But I do say it is the implementation of the er, there is an exception here, that is, MapperFactoryBean is a factory bean, factory beans have special purposes in Spring injection. During injection, the getObject () method of the factory class is called to inject the obtained object. This explains that the factory class is not instantiated, the cause of injection is that the factory class is indeed the implementation class of the ER interface. For details, refer to this Article)



The above problems are entangled in the latest official document, which does not explicitly mention dependency injection. What information is disclosed in an old document? I don't know. Maybe this latest document is not complete yet...

Mybatis latest documentation?



Old Mybatis document


Oh, the original document is MyBatis-Spring. The original document is different between the two... Ah!

It can be seen that you know more and won't use it. It's better than you don't know anything. If you really want to find information, you can also find a solution. This is the idea of solving the problem!

In normal times, it is actually a matter of thinking. In normal times, it is not that difficult to pull more eggs.




Related Article

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.