【问题标题】:The matching wildcard is strict, but no declaration can be found for element 'import'匹配的通配符是严格的,但找不到元素“import”的声明
【发布时间】:2017-10-20 17:11:11
【问题描述】:

我正在为我的 Maven Web 应用程序使用 Spring 安全性,并且我已经创建了 spring-security.xml 文件来处理身份验证。我正在尝试使用导入从另一个文件 Beans.xml 导入一个 bean。当我这样做时,发生了以下错误。

匹配的通配符是严格的,但是找不到元素'import'的声明

Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<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.xsd">

    <bean id = "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
      <property name = "url" value = "jdbc:mysql://localhost:3306/Employee_Management"/>
      <property name = "username" value = "root"/>
      <property name = "password" value = "root"/>
   </bean>

   <bean id = "transactionManager" 
      class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name = "dataSource"  ref = "dataSource" />
   </bean>

   <bean id = "employeeJDBCTemplate" class = "com.utility.EmployeeJDBCTemplate">
     <property name = "dataSource"  ref = "dataSource" />
     <property name = "transactionManager" ref = "transactionManager"/>
   </bean>

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

spring-security.xml

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

    <import resource="Beans.xml"/>

    <http entry-point-ref="loginUrlAuthenticationEntryPoint" auto-config="true" authentication-manager-ref="authenticationManager">
        <intercept-url pattern="/j_spring_security_check" access="isAnonymous()"/>
        <intercept-url pattern="/login" access="isAnonymous()"/>
        <intercept-url pattern="/welcome" access="hasRole('ROLE_ADMIN')"/>
        <form-login login-processing-url="/j_spring_security_check" login-page="/login" default-target-url="/welcome"
            authentication-failure-url="/login?error" username-parameter="username"
            password-parameter="password" />
        <logout logout-url="/j_spring_security_logout" invalidate-session="true" logout-success-url="/login?logout"/>
    </http>

    <beans:bean id="employeeAuthenticationProvider" class="com.authentication.EmployeeAuthenticationProvider">
      <beans:property name="employeeJDBCTemplate" ref="employeeJDBCTemplate" />
    </beans:bean>

    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="employeeAuthenticationProvider"/>
    </authentication-manager>

    <beans:bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:constructor-arg value="/login"/>
    </beans:bean>
</beans:beans>

这里我使用了导入的 bean 文件中的“employeeJDBCTemplate”bean。但是导入标签会产生错误。 spring-security.xml 和 Beans.xml 在同一个文件夹中可用。我还尝试将 Beans.xml 文件移动到资源文件夹。即使那样我也遇到了同样的错误。使用的 Spring 版本是 4.3.4,Spring Security 版本是 4.2.0。请帮忙。

【问题讨论】:

    标签: java xml spring maven spring-mvc


    【解决方案1】:

    spring-security.xml 中,默认命名空间是http://www.springframework.org/schema/security(“安全”命名空间)而不是http://www.springframework.org/schema/beans(“beans”命名空间)。 import 元素定义在 'beans' 命名空间中,因此要访问它,您必须使用 beans: 前缀限定 &lt;import&gt;

    试试

    <beans:import resource="Beans.xml"/>
    

    而不是

    <import resource="Beans.xml"/>
    

    【讨论】:

      猜你喜欢
      • 2012-08-26
      • 2021-11-02
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 2011-08-28
      • 2012-11-18
      • 2012-11-15
      相关资源
      最近更新 更多