java 必须为元素类型“beans”声明属性“xmlns”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15291602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attribute "xmlns" must be declared for element type "beans"
提问by ayushman999
I am new to the spring framework. I am trying out a tutorial that uses the spring @Async annotation.I am receiving this error Line 9 in XML document from class path resource [spring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 109; Attribute "xmlns" must be declared for element type "beans".
我是 spring 框架的新手。我正在尝试使用 spring @Async 注释的教程。我在来自类路径资源 [spring.xml] 的 XML 文档中收到此错误第 9 行无效;嵌套异常是 org.xml.sax.SAXParseException;行号:9;列数:109;必须为元素类型“beans”声明属性“xmlns”。
I want to know why this error is happening and how can it be resolved?
我想知道为什么会发生此错误以及如何解决?
My spring.xml file is below
我的 spring.xml 文件在下面
**
**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="cs"/>
<bean id="regularService" class="cs.RegularService">
</bean>
<task:annotation-driven/>
</beans>
**
**
RegularService.java
常规服务.java
package cs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cs.MailUtility;
@Service
public class RegularService {
@Autowired
private MailUtility mailUtility ;
public void registerUser(String userName){
System.out.println(" User registration for "+userName +" complete");
mailUtility.sendMail(userName);
System.out.println(" Registration Complete. Mail will be send after 5 seconds ");
}
}
MailUtility.java
邮件实用程序
package cs;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class MailUtility {
@Async
public void sendMail(String name){
System.out.println(" I Will be formatting html mail and sending it ");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" Asynchronous method call of send email — Complete ");
}
}
TestService.java
测试服务.java
package cs;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cs.RegularService;
public class TestService {
public static void main(String args[]){
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"spring.xml"});
RegularService regService = (RegularService) appContext.getBean("regularService");
regService.registerUser("Skill-Guru");
}
}
回答by
Remove this line:
删除这一行:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
Then you file is valid.
那么你的文件是有效的。
回答by Archana Shah
You are mixing the DTD and XSD declarations. Simply remove the line:
您正在混合 DTD 和 XSD 声明。只需删除该行:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
回答by lin
Your springmvc config.xml
has error.
你的 springmvcconfig.xml
有错误。
xmlns:task="http://www.springframework.org/schema/task"
xmlns:task="http://www.springframework.org/schema/任务"
Update it to mvc
将其更新为mvc