2013-03-08 987 views
2

我是Spring框架的新手。我正在尝试使用Spring @Async 批注的教程。我收到此错误来自class path资源[spring.xml]的XML文档中的第9行无效;嵌套异常是org.xml.sax.SAXParseException; lineNumber:9; columnNumber:109;必须为元素类型“beans”声明属性“xmlns”必须为元素类型“beans”声明属性“xmlns”

我想知道为什么发生这个错误,怎么解决?

我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

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

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"); 

} 

} 

回答

12

删除此行:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 

然后你的文件是有效的。

+0

去除线除去上述error.Now此特定错误已重新出现** org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.context.annotation.internalAsyncAnnotationProcessor'的Bean时出错:init方法的调用失败;嵌套的异常是java.lang.NoClassDefFoundError:org/aopalliance/aop/Advice ** – ayushman999 2013-03-08 10:26:17

+0

您需要添加spring aop jar – 2013-03-08 10:27:24

+0

并且您能告诉我为什么doctype必须被删除吗? – ayushman999 2013-03-08 10:27:37

0

您的springmvc​​3210有错误。

的xmlns:任务= “http://www.springframework.org/schema/ 任务

更新它MVC

+0

任务很好,因为我试图使用taskexecutor @Async – ayushman999 2013-03-09 06:24:08

相关问题