Java cvc-elt.1:找不到元素“beans”的声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27420395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:21:08  来源:igfitidea点击:

cvc-elt.1: Cannot find the declaration of element 'beans'

javaspringspring-security

提问by Asalas77

I am trying to get a bean object to authenticate the user login functionality with Spring Security:

我正在尝试获取一个 bean 对象以使用 Spring Security 对用户登录功能进行身份验证:

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

My applicationContext.xmlis below:

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>

but I get this exception:

但我得到这个例外:

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

I am having difficulty understanding the issue.

我很难理解这个问题。



Update更新

I have made some changes to my XML as such:

我对我的 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>

I now receive this exception:

我现在收到此异常:

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

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

采纳答案by Karthikeyan Vaithilingam

Your default namespace is http://www.springframework.org/schema/securityand you configured xmlns:beans="http://www.springframework.org/schema/beans"this means you have to add the prefix beans:to all the tag form http://www.springframework.org/schema/beansso your XML should be as follows.

您的默认命名空间是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>

回答by Enli

This code will help you.

此代码将帮助您。

<?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>

回答by Chinmoy

For me I just did cut and paste and save of XML files in same place and it worked for me !!

对我来说,我只是在同一个地方剪切、粘贴和保存 XML 文件,它对我有用!!

回答by Pancham Goyal

For me somehow the changes to the applicationContext.xml were not updated in the classpath. So I manually deleted the applicationContext.xml file from classpath and re-build the application which solved my problem

对我来说,对 applicationContext.xml 的更改没有在类路径中更新。所以我从类路径中手动删除了 applicationContext.xml 文件并重新构建了解决我问题的应用程序

回答by mannedear

I'm completely unsure why this wierd kind of behavior. I too faced the same exception and followed what Karthikeyan Vaithilingamhas advised. But still the issue is not resolved. So I have reverted the changes what ever I have done and saved the file. Viola!! Exception gone, no errors now.

我完全不确定为什么会有这种奇怪的行为。我也面临同样的例外,并遵循了Karthikeyan Vaithilingam建议。但问题仍然没有解决。所以我已经恢复了我所做的更改并保存了文件。中提琴!!异常消失,现在没有错误。