Для контекста элемента не найдено объявления: annotation-config '
в spring всякий раз, когда я пишу <context:annotation-config/>
в моем spring.xml, я получаю эту ошибку: -
Исключение в потоке "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: строка 81 в документе XML из ресурса пути класса [ spring.xml] недействительна; Вложенное исключение - org.xml.sax.SAXParseException; lineNumber: 81; columnNumber: 30; cvc-complex-type.2.4.c: Подстановочный шаблон является строгим, но для контекста элемента: annotation-config не найдено объявления.
И содержимое моего spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
xmlns:context="http://www.springframework.org/schema/context"
>
<bean id="circle" class="com.mysite.Circle">
</bean>
...
<context:annotation-config/>
Кто-нибудь скажет мне, где я иду не так????
Ответы
Ответ 1
Вы используете пространство имен XML (в данном случае контекст), не объявляя его
Измените свой xml на это:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
Вы также ссылались на http://www.springframework.org/schema/beans/spring-beans-4.0.xsd, который, как мне кажется, не существует.
Ответ 2
Если вы используете Spring 4.0, вот что я нашел:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
xmlns:context="http://www.springframework.org/schema/context">
Это сработало для меня. Найдено здесь: http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/
Ответ 3
Обратите внимание: при использовании пространства имен context
вы должны обновить атрибуты XML head 2:
-
xmlns:context
-
xsi:schemaLocation
.
Начинающие добавили только xmlns:context
и забыли около 2 новых записей xsi:schemaLocation
:
<beans xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
......
>
Ответ 4
Добавьте эти строки в свой XML файл.
<xmlns:context="http://www.springframework.org/schema/context"
и
<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">
.