Spring使用注解代替XML配置出现的问题

a_elegant_joker 2017-08-15 11:39:38
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);

}
}

...全文
156 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_elegant_joker 2017-08-15
  • 打赏
  • 举报
回复
引用 1 楼 lanapr 的回复:
你这种取对象的方法是读取配置文件的方法吧,要在配置文件里声明才行,想要取注解的应该是beansession工厂的方式取吧
不是很懂,可以说清楚点吗
a_elegant_joker 2017-08-15
  • 打赏
  • 举报
回复
不是很懂,可以说清楚点吗
lanapr 2017-08-15
  • 打赏
  • 举报
回复
你这种取对象的方法是读取配置文件的方法吧,要在配置文件里声明才行,想要取注解的应该是beansession工厂的方式取吧
a_elegant_joker 2017-08-15
  • 打赏
  • 举报
回复
<?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> 我的配置文件基本正确,eclipse也没有报错。但是把xmlns:beans="http://www.springframework.org/schema/beans"中的beans去掉之后,配置文件报错。 百度之后,说xmlns="http://www.springframework.org/schema/beans" 声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。 然后问题定位到我的base-paceage,写错了,package,阿西吧。。。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧