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

时间:2017-05-20 18:03:58

标签: java xml spring maven spring-mvc

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

匹配的通配符是严格的,但是找不到元素'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>

弹簧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安全版本是4.2.0。请帮忙。

1 个答案:

答案 0 :(得分:0)

spring-security.xml中,默认命名空间为http://www.springframework.org/schema/security(&#39;安全&#39;命名空间),而不是http://www.springframework.org/schema/beans(&#39; bean&#39;命名空间)。 import元素在&#39; bean&#39;中定义。名称空间,因此要访问它,您必须使<import>前缀符合beans:

尝试

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

而不是

<import resource="Beans.xml"/>