java - 必须为元素类型 "xmlns"声明属性 "beans"

标签 java spring asynchronous

我是 spring 框架的新手。我正在尝试使用 spring @Async 的教程 注释。我从类路径资源 [spring.xml] 收到此错误 XML 文档中的第 9 行无效;嵌套异常是 org.xml.sax.SAXParseException;行号:9;列数:109;必须为元素类型“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>

**

常规服务.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 ");
}

}

邮件实用程序.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 ");

}

}

测试服务.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");

}

}

最佳答案

删除这一行:

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

那么你的文件是有效的。

关于java - 必须为元素类型 "xmlns"声明属性 "beans",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15291602/

相关文章:

java - 自定义 HTTP 403 页面在 Spring Security 中不起作用

java - Java Spring Async性能

javascript - 数据库集群-异步任务

c# - 异步方法是否必须使用 "await"关键字?

java - 和之间的区别

Java 覆盖 hashCode() 方法有任何性能问题?

java - 如何进行延迟的非阻塞函数调用

java - Tomcat 在 CentO 上不起作用 - ExecStop=/bin/kill -15 $MAINPID (code=exited, status=1/FAILURE)

java - 创建名称为 'passwordEncoder' : Requested bean is currently in creation 的 bean 时出错

json - knockout JS : how to set the initial value from a dropdown list when data is retrieved asynchronously using JSON?