Attribute "xmlns" must be declared for element type "beans"

17,660

You are mixing the DTD and XSD declarations. Simply remove the line:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
Share:
17,660
ayushman999
Author by

ayushman999

Updated on June 09, 2022

Comments

  • ayushman999
    ayushman999 almost 2 years

    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".

    I want to know why this error is happening and how can it be resolved?

    My spring.xml file is below

    **

    <?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");
    
    }
    
    }
    
  • ayushman999
    ayushman999 about 11 years
    task is fine here as i am trying to use taskexecutor @Async