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