Spring Mapper injection into service failed solution

Source: Internet
Author: User

Spring Mapper injection into service failed solution

Background: Some time ago in the research Springboot, and the company's small projects successfully used Springboot transformation, for the springboot of the agreement than the concept of configuration, it is agreed that the project structure and code more concise and elegant. This time is not too busy, so the project is ready to change, now the project with the company's traditional SSM, the project structure screenshot as follows:


Personal Sense code and configuration is a bit bloated, refer to the idea of springboot, try to reduce the configuration file, optimize the code, make the project more concise and elegant.

1,controller configuration file Applicationcontext-mvc.xml

Controller configuration, instead of using traditional bean configuration, direct to scan injection

<!--1. Open annotation Scan-->
<!--SPRINGMVC's configuration file does not scan classes with @service annotations-->
<context:component-scan base-package= "Com.droi.web" >
<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/>
</context:component-scan>


<!--2. Supports all MVC annotations-->
<mvc:annotation-driven/>

<!--3. Configure the default URL processor-->
<mvc:default-servlet-handler/>

<!----below are some page view parsing and file upload configuration--->

Note: After configuration, remember to remove the bean configuration from the controller

2. Service configuration file Applicationcontext-service.xml

Now that the annotation is implemented, the configuration file can be killed, with @service annotations in the code



Note: You can also directly annotate with @service, but it is important to note that when injected inside the controller, you may be prompted for the injection failure, which needs to be the implementation class, not the interface

3, mapper configuration file Applicationcontext-mapper.xml

The project is MyBatis, different from the past, so there is no so-called DAO and Daoimpl, directly is Mapper.java and mapper.xml to achieve.

Note: Mapper.java is an interface, there is no implementation class, because mybatis when we get mappe, the dynamic agent based on the mapper.xml automatically generate an instantiated class, the real call is the proxy instantiation of the file

Ditto, Mapper also to use annotation implementation, then this configuration file can also be killed, in the code with @repository to annotate,

But you need to add the configuration to spring management to inject:



Service inside with @autowired injection mapper, but prompted error,

The error description is as follows: No matching bean of type [*.*. Usermapper] found for dependency:expected at least 1 bean which

Literally, it looks like spring didn't scan the big mapper, but it did configure the Mapper scan path:


Troubleshooting: Since doubt, not scanned, then directly using the configuration to obtain

Applicationcontext.getbean ("Usermapper");

This can be obtained, then it is scanned, but the annotation fails.

Questions continue to be checked: will not be the injection mode problem.

Baidu @Autowired and @Resource, and then @autowired should be @resouce, incredibly successful. So continue to analyze the reason

Baidu is as follows:

(1) @Autowired annotations assemble dependent objects by type (Bytype), by default it requires dependent objects to exist, and if null values are allowed, it can be set required property to False. If we want to use the assembly by name (ByName), we can use it in conjunction with @qualifier annotations. As follows: @Autowired @Qualifier ("Usermapper") private usermapper usermapper; (2) @Resource @Resource by default according to ByName automatic injection, provided by the Java EE, need to import package Javax.annotation.Resource. @Resource has two important properties: Name and type, and spring resolves the name property of the @resource annotation to the name of the bean, and the Type property resolves to the bean type. Therefore, if you use the Name property, you use the ByName automatic injection policy, and the Type property uses the Bytype automatic injection policy.        If neither the name nor the type attribute is established, the byname is automatically injected using the reflection mechanism. So the problem, seemingly found, we @repository ("Usermapper"), assigned a name to manage (byname) to spring management, and @autowired by default by type (Bytype) to inject the object, recall < Bean type= "..." >

So, when you switch to @resouce, spring can also inject success by name (ByName).

If you really want to use @autowired to inject, so long must work with @qualifier, directly set the name

So far, mapper injection failure problem, has been resolved, the first blog, such as what is wrong or understand the wrong place, you are welcome to correct, if it is helpful to everyone, but also welcome to reprint, but please specify the address, thank you, Exchange progress OH

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.