我想获得有关Spring BeanCreationException错误的建议

时间:2017-08-02 15:18:19

标签: spring spring-mvc dependency-injection spring-bean

这是刚问过这个问题的人。

问题上的错误是成功的,但我遇到了另一个错误。

作为春天的初学者,有很多我不知道的事情和

我有很多问题要问。请理解。

错误代码。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.IMemberDAO service.MemberService.memberDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.IMemberDAO] 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)}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [C:\Users\admin\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\rachelivf\WEB-INF\classes\dao\mapper\memberDaoMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'member'.  Cause: java.lang.ClassNotFoundException: Cannot find class: member
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)

以下是MemberService.java代码。

package service;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;


import dao.IMemberDAO;
import model.Member;

@Service
public class MemberService  
{
    @Autowired
    private IMemberDAO memberDao;

    public void joinMember(HashMap<String, Object> params)
    {
        if(params.get("pw").equals(params.get("pwd_CHECK")))
        {
            memberDao.insertMember(params);
        }
    }
}

这是applicationContext.xml代码。

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="service" />

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <property value="com.mysql.jdbc.Driver" name="driverClassName"></property>
        <property value="jdbc:mysql://localhost/rachelvf" name="url"></property>
        <property value="root" name="username"/>
        <property value="mysql" name="password"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath*:dao/mapper/*.xml"></property>
    </bean>

    <bean id="memberDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        <property name="mapperInterface" value="dao.IMemberDAO"></property>
    </bean>

</beans>

据我所知,

这就是出现上述错误的原因,因为

我没有插入注释服务。

但显然我插入了Service注释,但是我收到错误。

请给我建议。

是memberDaoMapper代码。

<?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="dao.IMemberDAO">
    <insert id="insertMember" parameterType="java.util.HashMap">
        insert into member values
        (
            #{id},#{pw},default     
        )
    </insert>

    <update id="updateMember" parameterType="java.util.HashMap">
        update member set id = #{id}, pw = #{pw}, authority = #{authority}
    </update>

    <delete id="deleteMember" parameterType="String">
        delete from member where id = #{id}
    </delete>

    <select id="selectOne" parameterType="String" resultType="member">
        select * from member where id = #{id}
    </select>

    <select id="selectAll" parameterType="String" resultType="member">
        select * from member
    </select>
</mapper>

这是IMemberDao.java代码。

package dao;

import java.util.HashMap;

import java.util.List;

import org.mybatis.spring.annotation.MapperScan;

import model.Member;

public interface IMemberDAO 
{
    public int insertMember(HashMap<String, Object> params);

    public int updateMember(HashMap<String, Object> params);

    public int deleteMember(String id);

    public HashMap<String, Object> selectOne(String id);

    public List<HashMap<String, Object>> selectAll();
}

我彻底检查了错字,但没有找到拼写错误。

问题是什么?

1 个答案:

答案 0 :(得分:1)

这部分堆栈跟踪应该会给你一个正确的问题提示:

org.apache.ibatis.type.TypeException: Could not resolve type alias 'member'

如果没有映射器文件,我认为其中一个文件中存在错误,可能您在需要完全限定类名的XML属性中编写了member

我的建议是在这些文件中搜索member,您应该在不正确的字段中找到它(主要是parameterTyperesultType

查看您添加的映射文件,您有resultType="member"。如上所述,resultType需要一个完全限定的类名。 您的问题的解决方案主要是2:

  1. resultType修改为实际的课程

    <select id="selectOne" parameterType="String" resultType="model.Member">
        select * from member where id = #{id}
    </select>
    
    <select id="selectAll" parameterType="String" resultType="model.Member">
        select * from member
    </select>
    
  2. 为sampleVO类添加类型别名标记。

    <mapper namespace="model.Member">
         <typeAlias alias="member" type="model.Member" />
        ...
    </mapper>