前言

小编我将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注一下! 也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!让我们在成长的道路上互相学习,让我们共同进步,欢迎关注!

测试类UserTest:

package com.obtk.spring;

import com.obtk.xmlspring.service.UserService;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:applicationContext.xml")

public class UserServiceTest {

private UserService userService;

@Resource(name="userServiceNormal")

public void setUserService(UserService userService) {

this.userService = userService;

}

@Test

public void test(){

userService.add();

}

}

运行时出现以下报错:

错误分析: 

1:首先可以看到: 

 java.lang.IllegalStateException: Failed to load ApplicationContext

是说我们的ApplicationContext文件加载失败  

2:其中还有一句关键的:

Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreEsception:Line 5 in XML document from class path resouce

说明我们的xml文件中第5行出现错误

而小编反复的重试,摸索都出现以上错误

解决方法:

原来的XML配置文件:

将resources文件下的applicationContext.xml文件重新配置,并命名为:userServiceXMlApplication.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

总结:

出现以上报错可能的原因:

1:xml配置文件没有配置好 

2:测试类中@ContextConfiguration(locations = "classpath:applicationContext.xml")

中的路径写错了

本小编可能是因为xml配置文件没有配置好

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: