16

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

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

My applicationContext.xml is below:

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

10 Answers 10

13

Your default namespace is http://www.springframework.org/schema/security and 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/beans so your XML should be as follows.

<?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>
4
  • I repleaced my code with yours and removed alias tags because it was causing errors, but now I get this Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]
    – Asalas77
    Commented Dec 11, 2014 at 10:37
  • Are you having spring-security-config jar in your class path? Commented Dec 11, 2014 at 10:39
  • I remember adding it to pom.xml but it was not there, so I added it. Now I have made some changesand updated the question. Please take a look.
    – Asalas77
    Commented Dec 11, 2014 at 11:20
  • 1
    Again! use <beans:import> instead of <import> Commented Dec 11, 2014 at 11:22
4

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

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

3

I had similar issue and I want to give another example of possible causes.

Original .xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans"
        xmlns:context="https://www.springframework.org/schema/context" xmlns:tx="https://www.springframework.org/schema/tx"
        xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
            https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
            https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
            https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

working .xml:

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

Solutions:

  1. use xmlns="http://www.springframework.org/schema/beans" as base xmlns. remove beans: prefix
  2. change https to http
1

After ticking the option in maven it worked for me.

In Eclipse :

Go to top bar : Window -> Preference -> Maven -> tick the option ('download repository index on start up').

Then 'Apply & Close'

The error may be because of not adding spring-core in the java build path. If you haven't added the spring-core, atleast we need to allow maven to download and add to build path.

0

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

0

I'm completely unsure why this wierd kind of behavior. I too faced the same exception and followed what Karthikeyan Vaithilingam has 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.

0

I got same error while trying to create executable jar. I had

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml");

instead of (notice the * after classpath)

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:context.xml");
0

This is simplistic bean definition worked for my Spring Mvc web app

  • springframework : 4.1.8.RELEASE (Scope:compile)
  • Eclipse version: 2019-12 (4.14.0)
  • Jre : 1.8
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        ">
    <context:component-scan base-package="<your-pkg-name>"></context:component-scan>
</beans>
-1

use this:

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
</beans>

in case if it is not worked:

If you are using Eclipse :

Go to top bar : Window -> Preference -> Maven -> tick the option ('download repository index on start up').

Thats is 'Apply & Close'.

Not the answer you're looking for? Browse other questions tagged or ask your own question.