Контекст: компонент-сканирование "не связан
Я новичок в spring, и я знаю, что этот вопрос задавался много раз, но мне пришлось снова его спросить.
Я предполагаю, что я сделал соответствующие объявления пространства имен, но все еще столкнулся с ошибкой "The prefix "context" for element "context:component-scan" is not bound."
Здесь есть аналогичный вопрос , но я получаю ответ
Вот мой 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-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="point1" class="org.sri.proj.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
<bean id="point2" class="org.sri.proj.Point">
<property name="x" value="10" />
<property name="y" value="10" />
</bean>
<context:component-scan base-package="org.sri.proj"/>
</beans>
Ответы
Ответ 1
Добавьте объявление пространства имен context
в определение тега beans
в файле контекста приложения
<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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
Ответ 2
Да, вы должны добавить
http://www.springframework.org/schema/context
До
http://www.springframework.org/schema/context/spring-context-3.0.xsd
Так выглядит:
<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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
Ответ 3
Вам не хватает пространства имен Context (http://www.springframework.org/schema/context
):
<beans:beans xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
Добавьте последнюю строку из этого кода.
Ответ 4
За все время Саке не просто копировать и проезжать. Но.
Сначала посмотрим, какой комментарий вы использовали в вашем XML файле. Скажем, ваш xml содержит следующее...
-
<context:component-scan base-package="com.spring.study" />
-
<context:annotation-config/>
-
<mvc:annotation-driven />
Затем перед копированием и прошлым кодом посмотрите, что вам действительно нужно в заголовке <beans:beans section ...
Итак, сделайте это так, чтобы он соответствовал вышеуказанной настройке конфигурации и
Убедитесь, что вы очищаете и создаете каждый раз после изменения файла!
<?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:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.spring.study" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Ура! Dman!
Ответ 5
Вы должны добавить xmlns:context="http://www.springframework.org/schema/context"
в ваш beans xml.