SPRINGMVC MyBatis Consolidated DAO Interface (Mapper interface) Spring injection failed

Source: Internet
Author: User

When consolidating Springmvc and MyBatis, the start service has the following error:

Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying Bean of type [demo.dao.PersonMapper] Found for dependency:expected at least 1 beans which qualifies as autowire for this candidate. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)}

Wrong emphasis singled out:
Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying Bean of type [demo.dao.PersonMapper] Found for dependency

Personmapper the implementation object for this interface is not injected.

My servicer interface implements the class code:

@Service ("Personservice") Public  
class Personserviceimpl implements Ipersonservice {

    @Autowired  
    private Personmapper Personmapper;  

    @Override Public Person
    Querybyid (Integer ID) {return
        This.personMapper.selectByPrimaryKey (ID);
    }

}

The @autowired Personmapper caused the error in this section of the code at startup, and no object was found in the container.
The cause of the error may occur:
1. The following annotation configuration was not added to the configuration file: (I added)

<!--support for the default annotation mappings-->
    <mvc:annotation-driven/>
    <!--Enable automatic scanning  -->
    <context: Component-scan base-package= "demo.*"/>

2, then is not a profile error caused the file did not load, causing no annotations to come in. So I changed the code to the next code:

@Service ("Personservice") Public  
class Personserviceimpl implements Ipersonservice {

    private personmapper Personmapper;  

    @Override Public Person
    Querybyid (Integer ID) {

        //load XML file
        applicationcontext  ac = new Classpathxmlapplicationcontext ("Classpath:application/applicationcontext-datasource.xml");
        Personmapper = (personmapper) ac.getbean ("Personmapper");

        return This.personMapper.selectByPrimaryKey (ID);
    }

OK, you can run, stating that there is no problem with the configuration file.
So before that is not loaded Applicationcontext-datasource.xml file, so I changed a key value in the Jdbc.properties file (referenced in the XML file), and then the service implementation class in the relevant code deleted, start to see whether the report Wrong, if the error description loaded the XML file, if not the error description does not load the file. Result Wood has the error ...

So that wood has loaded applicationcontext-datasource.xml file.

Here's my web.xml configuration:

<!--load Spring configuration file-->
    <context-param>
        <param-name>contextconfiglocation</param-name >
        <param-value>
            classpath*:application/applicationcontext*.xml
        </param-value>
    </context-param>

The following is my database MyBatis related configuration:

 

Careful examination of these two configurations, feel no problem, and then see Web.xml contextconfiglocation This is how to load the specified configuration file, and then Baidu, the solution:
To add a context listener in Web.xml

<!--context Listener-->
    <listener>
        <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class>
    

About Contextconfiglocation Understanding clicks: Detailed Contextconfiglocation

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.