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>