gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:57:04 26 4
gpt4 key购买 nike

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

    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>

但我得到了这个异常(exception):

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>

我现在收到这个异常:

行号:11;列号:44; cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素'import'的声明。

最佳答案

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

关于java - cvc-elt.1 : Cannot find the declaration of element 'beans' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com