Подтвердить что ты не робот

Cvc-elt.1: Не удается найти объявление элемента 'beans'

Я пытаюсь получить объект bean для аутентификации функций входа пользователя с помощью Spring Безопасность:

    ApplicationContext context = new ClassPathXmlApplicationContext(
            "com/humandevice/drive/fx/util/applicationContext.xml");
    authenticationManager = (AuthenticationManager) context
            .getBean("authenticationManager");

Мой applicationContext.xml находится ниже:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.humandevice.drive.fx">
    <context:include-filter type="regex"
        expression="com.humandevice.drive.fx.*" />
</context:component-scan>
<bean id="LoginController" alias="loginController" class="controller.LoginController">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="applicationContext" ref="applicationContext" />
</bean>
<bean id="applicationContext" alias="applicationContext"
    class="org.springframework.context.ApplicationContext;">
</bean>
<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userService">
        <password-encoder ref="bCryptPasswordEncoder" />
    </authentication-provider>
</authentication-manager>
 </beans>

но я получаю это исключение:

Caused by: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 64; cvc-elt.1: Cannot find the declaration of element 'beans'.

Мне трудно понять проблему.


Обновить

Я внесли некоторые изменения в свой XML как таковой:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <import resource="/context-service.xml" />
    <import resource="/context-repository.xml" />

    <context:component-scan base-package="com.humandevice.drive.fx"></context:component-scan>

    <authentication-manager>
        <authentication-provider user-service-ref="com.humandevice.drive.service.user.IUserService">
            <password-encoder ref="bCryptPasswordEncoder" />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

Теперь я получаю это исключение:

lineNumber: 11; columnNumber: 44; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'import'.

4b9b3361

Ответ 1

Ваше пространство имен по умолчанию - http://www.springframework.org/schema/security, и вы настроили xmlns:beans="http://www.springframework.org/schema/beans", это означает, что вы должны добавить префикс beans: ко всей форме тега http://www.springframework.org/schema/beans, чтобы ваш XML был следующим.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
       xmlns:beans="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.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:context="http://www.springframework.org/schema/context">
    <context:component-scan base-package="com.humandevice.drive.fx">
        <context:include-filter type="regex"
                                expression="com.humandevice.drive.fx.*" />
    </context:component-scan>
    <beans:bean id="LoginController" alias="loginController" class="controller.LoginController">
        <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="applicationContext" ref="applicationContext" />
    </beans:bean>
    <beans:bean id="applicationContext" alias="applicationContext"
          class="org.springframework.context.ApplicationContext;">
    </beans:bean>
    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="userService">
            <password-encoder ref="bCryptPasswordEncoder" />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

Ответ 2

Этот код поможет вам.

<?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" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="
http://www.springframework.org/schema/beans 
classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
classpath:/org/springframework/context/config/spring-context-3.0.xsd
http://www.springframework.org/schema/aop 
classpath:/org/springframework/aop/config/spring-aop-3.0.xsd
">      
</beans>

Ответ 3

Для меня я просто вырезал и вставлял и сохранял файлы XML в одном месте, и это сработало для меня.

Ответ 4

Для меня как-то изменения в applicationContext.xml не были обновлены в classpath. Поэтому я вручную удалил файл applicationContext.xml из classpath и пересобрал приложение, которое решило мою проблему

Ответ 5

Я совершенно не уверен, почему это странное поведение. Я тоже столкнулся с тем же исключением и следовал Karthikeyan Vaithilingam. Но все же проблема не решена. Поэтому я отменил изменения, которые когда-либо делал, и сохранил файл. Viola !! Исключение прошло, ошибок нет.