Spring使用注解代替XML配置出现的问题
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 7 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 535; cvc-elt.1: 找不到元素 'beans' 的声明。
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop/spring-framework-4.2.4.RELEASE\schema\aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop/spring-framework-4.2.4.RELEASE\schema\aop http://www.springframework.org/schema/aop/spring-framework-4.2.4.RELEASE\schema\aop\spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<context:component-scan base-paceage="cn.itcast.bean"></context:component-scan>
</beans>
User对象
@Component("User")
public class User {.....}
测试程序
package cn.itcast.a_annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.User;
public class Demo {
@Test
public void fun() {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User) ac.getBean("User");
System.out.println(u);
}
}