Configuration problem: Failed to import bean definitions from URL location classpath:spring-mvc.xml

简介: Configuration problem: Failed to import bean definitions from URL location classpath:spring-mvc.xml

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration

problem: Failed to import bean definitions from URL location [classpath:spring-mvc.xml]

配置问题:无法从 URL 位置导入 Bean 定义


排错思路:

  • 查看bean是否注入成功
<!--将所有业务类注入Spring,可以通过配置或者注解-->
<bean id="BookServiceImpl" class="com.wei.service.BookServiceImpl">
    <property name="bookMapper" ref="bookMapper"/>
</bean>
  • Junit单元测试,看代码是否能查询出来结果
import com.wei.pojo.Books;
import com.wei.service.BookServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    @Test
    public void test() {
        //解析applicationContext.xml文件,生成管理相应的Bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //getBean:参数即为Spring配置文件中的bean的id
        BookServiceImpl bookServiceImpl = (BookServiceImpl) context.getBean("BookServiceImpl");
        for (Books books : bookServiceImpl.queryAllBook()) {
            System.out.println(books);
        }
    }
}

如果查询出结果,说明问题出现在Spring

SpringMVC整合的时候没有调用service层的bean

applicationContext.xml没有注入bean

web.xml中绑定配置文件,配置的是spring-mvc.xml,没有service的bean

web.xml

<param-value>classpath:applicationContext.xml</param-value>

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>
</beans>
目录
相关文章
|
7天前
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
15 2
|
7天前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
17 3
|
1月前
|
安全 Java Spring
Spring框架中的单例Bean是线程安全的吗?
Spring框架中的单例Bean是线程安全的吗?
28 1
|
2天前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
11 2
|
6天前
|
前端开发 Java 开发者
在Spring框架中,`PathMatcher`是用于进行URL路径匹配的接口
在Spring框架中,`PathMatcher`是用于进行URL路径匹配的接口
25 6
|
7天前
|
Java 开发者 Spring
Spring 中 Bean 的生命周期
Spring 中 Bean 的生命周期
11 2
|
1月前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
30 3
Spring的加载配置文件、容器和获取bean的方式
|
1月前
|
Java Spring 容器
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
35 1
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
|
14天前
|
Java Spring
解决 Spring 中 Prototype Bean 注入后被固定的问题
【6月更文挑战第8天】学习 Spring 框架内不原理的意义就是,当遇到问题时,分析出原因,就可以从多个切入点,利用 Spring 的特性,来解决问题。
26 2
|
24天前
|
Java Spring 缓存
Spring Bean循环依赖详解
【6月更文挑战第2天】
26 2