本文介绍了Spring3 Mybatis 3映射器未找到此类bean的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Spring 3 + Mybatis3 + mysql,但没有解决问题,几乎所有教程都遇到了相同的异常,也许有人可以帮助我

I am trying Spring 3 + Mybatis3 + mysql but didn't work out, tried almost all of the tutorials getting same exception, may be some one here can help me out

以下是错误跟踪输出,我还附上了源代码列表,请帮帮我

Following is the Error trace output and i have also attached list down the source code please help me out

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.triage.persistance.UserService com.triage.persistance.UserServiceImpl.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.triage.persistance.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
        at 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 for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
        ... 41 more

** User.java **

** 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 **

** 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 FROM USER
    </select>
</mapper>

** sqlmap-config.xml **

** 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 from the defaults -->
       <setting name="lazyLoadingEnabled" value="false" />
    </settings>
    <typeAliases>
        <typeAlias type="com.triage.domain.User" alias="user"/>
    </typeAliases>
</configuration>

** UserService.java **

** 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>

**我的测试用例执行点**

** My 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 i try this successfully found this object

    @Test
    public void testHelloAccount() {
        List<User> items = userService.getAllUsers();
        System.out.println(items.size());
        Assert.assertTrue(!items.isEmpty());
        Assert.assertTrue(true);
    }


}

推荐答案

在Spring上下文文件中添加以下两个配置

following two configurations into your spring context file

使用spring-mybatis版本1.2.0

use spring-mybatis version 1.2.0

更改:更改sqlSession工厂以读取映射器文件

Change: Change your sqlSession factory to read your 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>

添加以下条目

<!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.comcast.triage.persistance" />
    </bean>

这篇关于Spring3 Mybatis 3映射器未找到此类bean的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:25