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

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

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>

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>

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

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:
use xmlns="http://www.springframework.org/schema/beans" as base xmlns. remove beans: prefix
change https to http

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

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.

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");

Related

Failed to read schema document http://www.springframework.org/schema/beans/spring-context.xsd

I was getting the following error message while i was working with Spring with IntelliJ:
Failed to read schema document
'http://www.springframework.org/schema/beans/spring-context.xsd',
because 1) could not find the document; 2) the document could not be
read; 3) the root element of the document is not .
Help on this.
I was using wrong url in xsi:schemaLocation
Instead if using:
http://www.springframework.org/schema/beans/spring-context.xsd
I should have to used:
http://www.springframework.org/schema/context/spring-context.xsd
That solved my problem.
I crossed checked this with firing url into browser where within beans no spring-context.xsd was available.
Hope some of you can help with this.
#Indrajeet : The proper spring.xml or application-context.xml format :
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.demo" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

Invalid content was found starting with element 'authentication-manager'

I get error with spring configuration:
Invalid content was found starting with element 'authentication-manager'. One of '{"http://www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org/schema/beans":bean, WC[##other:"http://www.springframework.org/schema/beans"]}' is expected.
Here is the bean xml file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.rsc."/>
<mvc:annotation-driven />
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsServiceImpl">
<password-encoder ref="encoder"></password-encoder>
</authentication-provider>
</authentication-manager>
<bean id="userDetailsServiceImpl" class="com.rsc.service.UserDetailsServiceImpl"></bean>
<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<constructor-arg name="strength" value="11"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
I have added all the required bean configuration but still I am getting error. What I am i missing in the configuration?
I think you need to add the spring security namespace.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
[...]
<security:authentication-manager>
...
</security:authentication-manager>
[...]
</beans>
since beans is your default namespace spring tries to find the authentication-manager there. Therefore you need to introduce the security namespace. See the documentation for further information: http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#d0e507

Spring #Async. XML configuration

I need to configure one asynchronous method in my project. I trying to it it this way:
This is my spring config:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
"
>
<context:annotation-config/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<task:executor id="asyncExecutor" pool-size="5" />
<task:annotation-driven executor="asyncExecutor" />
and the method:
#Async
public Future<Boolean> async() throws InterruptedException {
TimeUnit.SECONDS.sleep(20);
return new AsyncResult<>(true);
}
When I run test, I get an Exception:
nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.
If I delete <task:annotation-driven executor="asyncExecutor" /> then method doesn't start asynchronously.
What wrong with my config?

The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'

My Code:
I have configured XML correctly, added all schemas but still getting error.
Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/spring-context-3.2.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config />
<context:component-scan base-package="com"/>
<bean id="bean1" class="com.pojos.BookInfo">
</bean>
</beans>
You have a mistake in specifying the location for the XSD for xmlns:context="http://www.springframework.org/schema/context" namespace:
Replace
http://www.springframework.org/schema/spring-context-3.2.xsd
with
http://www.springframework.org/schema/context/spring-context-3.2.xsd
Altogether:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config />
<context:component-scan base-package="com"/>
<bean id="bean1" class="com.pojos.BookInfo">
</bean>
</beans>

Errors of the Cineasts examples of Spring data neo4j

I imported the cineast maven project to eclipse, but I face a configuration problem...
cvc-complex-type.4: Attribute 'base-package' must appear on element 'neo4j:config'
Below are the configuration files which have this error.
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.neo4j.cineasts">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:spring-configured/>
<neo4j:config storeDirectory="target/data/graph.db"/>
<neo4j:repositories base-package="org.neo4j.cineasts.repository"/>
<!--neo4j:config graphDatabaseService="graphDatabaseService"/>
<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg value="http://localhost:7474"/>
</bean-->
<bean class="org.neo4j.cineasts.movieimport.MovieDbApiClient">
<constructor-arg value="926d2a79e82920b62f03b1cb57e532e6"/>
</bean>
<bean class="org.neo4j.cineasts.movieimport.MovieDbLocalStorage">
<constructor-arg value="data/json"/>
</bean>
<tx:annotation-driven mode="proxy"/>
</beans>
movie-test-context.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="org.neo4j.cineasts"/>
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<neo4j:repositories base-package="org.neo4j.cineasts.repository"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<bean class="org.neo4j.cineasts.movieimport.MovieDbApiClient">
<constructor-arg value="926d2a79e82920b62f03b1cb57e532e6"/>
</bean>
<bean class="org.neo4j.cineasts.movieimport.MovieDbLocalStorage">
<constructor-arg value="data/json"/>
</bean>
<tx:annotation-driven mode="proxy"/>
</beans>
Have a look at http://blog.neo4j.org/2014/03/spring-data-neo4j-progress-update-sdn-3.html
This is actually a duplicate question from earlier on Stack, but, long and short is that if you're using the latest SDN (and I'm pretty sure you are), then you need to include the "base-package" attribute in both the neo4j:config and neo4j:repositories elements.
In the link above, Michael Hunger explains this and you can clearly see what the values for those attributes should be.
HTH

Categories

Resources