java - Spring3 mybatis 3 mapper no such bean found exception -
i trying spring 3 + mybatis3 + mysql didn't work out, tried of tutorials getting same exception, may 1 here can me out
following error trace output , have attached list down source code please me out
caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private com.triage.persistance.userservice com.triage.persistance.userserviceimpl.userservice; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.triage.persistance.userservice] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:506) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:284) ... 39 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.triage.persistance.userservice] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:924) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:793) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:707) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:478) ... 41 more
** user.java **
package com.triage.domain; public class user { private long id; private string name; private string standard; private int age; private string sex; public long getid() { return id; } public void setid(long id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getstandard() { return standard; } public void setstandard(string standard) { this.standard = standard; } public int getage() { return age; } public void setage(int age) { this.age = age; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } }
** userservice.xml**
<?xml version="1.0" encoding="utf-8"?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.triage.persistance.userservice"> <resultmap id="userresult" type="user"> <result property="id" column="id" /> <result property="name" column="name" /> <result property="standard" column="standard" /> <result property="age" column="age" /> <result property="sex" column="sex" /> </resultmap> <select id="getallusers" resultmap="userresult"> select id, name, standard, age, sex user </select> </mapper>
** sqlmap-config.xml**
<?xml version="1.0" encoding="utf-8"?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- changes defaults --> <setting name="lazyloadingenabled" value="false" /> </settings> <typealiases> <typealias type="com.triage.domain.user" alias="user"/> </typealiases> </configuration>
** userservice.java **
package com.triage.persistance; import java.util.list; import com.triage.domain.user; public interface userservice { public list<user> getallusers(); }
** test-application context **
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <aop:aspectj-autoproxy /> <context:component-scan base-package="com.triage" /> <context:property-placeholder location="classpath:properties/jdbc.properties" /> <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource" destroy-method="close"> <property name="driverclass" value="${jdbc.driverclassname}" /> <property name="jdbcurl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <tx:annotation-driven transaction-manager="txmanager" /> <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> <!-- jdbc template --> <bean id="jdbctemplate" class="org.springframework.jdbc.core.jdbctemplate"> <property name="datasource"> <ref bean="datasource" /> </property> </bean> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="configlocation" value="classpath:sqlmap-config.xml" /> <property name="mapperlocations" value="classpath:mappers/*.xml" /> </bean> </beans>
** test case execution point **
package com.triage.test; import java.util.list; import org.junit.assert; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.testexecutionlisteners; import org.springframework.test.context.junit4.abstractjunit4springcontexttests; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.support.dependencyinjectiontestexecutionlistener; import com.triage.domain.user; import com.triage.persistance.userservice; @runwith(springjunit4classrunner.class) @contextconfiguration(locations = "classpath:test-applicationcontext.xml") @testexecutionlisteners(value = { dependencyinjectiontestexecutionlistener.class }) public class helloportlettestcontext extends abstractjunit4springcontexttests { @autowired private userservice userservice; ** error no such bean found error // // @autowired // private sqlsessionfactory sqlsessionfactory; // if try found object @test public void testhelloaccount() { list<user> items = userservice.getallusers(); system.out.println(items.size()); assert.asserttrue(!items.isempty()); assert.asserttrue(true); } }
following 2 configurations spring context file
use spring-mybatis version 1.2.0
change: change sqlsession factory read mappers files
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="configlocation" value="classpath:mybatis/sqlmap-config.xml" /> <property name="mapperlocations" value="classpath:mybatis/mappers/*.xml" /> </bean>
add following entry
<!-- scan mappers , let them autowired --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.comcast.triage.persistance" /> </bean>
Comments
Post a Comment