I can´t import bean definitions from relative location - java

I'm getting the next error when I import context-cfg.xml in applicationContext.xml.
GRAVE: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem:
Failed to import bean definitions from URL location [classpath:/context-cfg.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from
class path resource [context-cfg.xml]; nested exception is java.io.FileNotFoundException: class path
resource [context-cfg.xml] cannot be opened because it does not exist
aplicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="classpath:/context-cfg.xml" /> //this line throws the error
</beans>
The structure of my project is as follows
Can any one help me?

From the official documentation of Maven : Maven project structure
Within artifact producing source directories (ie. main and test), there is one directory for the language java (under which the normal package hierarchy exists), and one for resources (the structure which is copied to the target classpath given the default resource definition).
So you have two options :
Option 1 :
change the location of your file to Java resources
Option 2 :
change statement like this <import resource="classpath:/resources/context-cfg.xml" />
In this case, your code will work fine in eclipse env, but during your maven build you need to add the resource folder to your classpath

Related

Spring Tomcat : Non-whitespace characters are not allowed in schema elements . Saw '301 Moved Permanently'

I am trying to bring up a tomcat server, and am facing problems with loading definitions of spring beans. The applicationContext.xml reads as follows :
<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:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xmlns:hz="http://www.hazelcast.com/schema/spring"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:solr="http://www.springframework.org/schema/data/solr" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
http://www.hazelcast.com/schema/spring META-INF/hazelcast-spring-2.0.xsd
http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr.xsd
http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd">
..
..
..
..
The IDE (Idea) says it is unable to resolve the file hazelcast-spring-2.0.xsd
The error log says:
13:19:59,909 INFO [localhost-startStop-1][XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
13:20:02,964 ERROR [localhost-startStop-1][ContextLoader] - Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 2 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://hazelcast.com/schema/spring/hazelcast-spring-2.0.xsd; lineNumber: 2; columnNumber: 35; s4s-elt-character:
**Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw '301 Moved Permanently'.**
What am I doing wrong here ? I did not touch this file.
I solved it. The problem was that the hazelcast schema version mentioned in the applicationContext.xml "http://www.hazelcast.com/schema/spring META-INF/hazelcast-spring-2.0.xsd" was different than the actual jar I was using, I think mine was 2.4 , so changing it to "http://www.hazelcast.com/schema/spring META-INF/hazelcast-spring-2.4.xsd" solved the problem.
The explanation is that spring is first searching the xsds in the jars and then on internet. So because of the wrong version it was not finding it in the jar, but was hitting the internet.
Had the same problem for http://www.hazelcast.com/schema/spring/hazelcast-spring-3.5.xsd and solved it by adding hazelcast-all with version 3.5 in the maven dependencies rather than simply hazelcast.
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>3.5</version>
</dependency>

How to load xml to ClassPathXmlApplicationContext in java without errors?

I have xml file to java to configure i can't load this that files in beans javas
main.java:
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
PDF2HTMLService service = appContext.getBean(PDF2HTMLService.class);
Error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: org.springframework.context.support.ClassPathXmlApplicationContext cannot be converted to org.springframework.context.ApplicationContext
at pdf2html.Main.main(Main.java:27)
Java Result: 1
how can i solve this problems...
XML FILE:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="pdf2html" />
</beans>
Very strange. ClassPathXmlApplicationContext implements ApplicationContext. Might be a classpath issue.
The ClassPathXmlApplicationContext is loaded from one jar, but ApplicationContext from another and they aren't compatible by versions.
Check you classpath for duplication of spring-context-[VERSION].jar

Running maven project on tomcat

I'm using tomcat 7 and when I run my web application(maven project), it gives me this error:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:hr_demo-security-context.xml]
Anyone can help? Thanks in advance.
hr_demo-security-context.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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans:beans>
There is syntax problem with your hr_demo-security-context.xml.
Either your in-code reference to the XML file is no longer referencing the file after deployment (due to the file being moved after packaging) or the xml file you have posted is being referenced, but by having no entries within it Spring is getting confused.

JAX-WS Schema http://jax-ws.dev.java.net/spring/servlet.xsd not able to be found

I am implementing JAX-WS with Spring framework.
The following is my Spring applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.dev.java.net/spring/servlet.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
However, Eclipse is complaining:
Referenced file contains errors (http://jax-ws.dev.java.net/spring/servlet.xsd).
After investigation, I find the URL:
http://jax-ws.dev.java.net/spring/servlet.xsd
Does not exist.
Instead, it seems be move to:
http://jax-ws.java.net/spring/servlet.xsd
(You can open this link in the brower)
Therefore, I updated XSD schema URL from
http://jax-ws.dev.java.net/spring/servlet.xsd
to
http://jax-ws.java.net/spring/servlet.xsd
Now my applicationContext.xml looks like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.java.net/spring/servlet.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
Actually with this change the Eclipse error goes away.
The problem is after launching the web service in Tomcat 7, I get the following runtime error instead:
org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 29; schema_reference.4: Failed to read schema document 'http://jax-ws.java.net/spring/servlet.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(ErrorHandlerWrapper.java:99)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:433)
Please advise.
Thank you very much.
Regards,
Your problem is that you change the location from http://jax-ws.dev.java.net/spring/servlet.xsd to http://jax-ws.java.net/spring/servlet.xsd. Even though the latter one is the right url, it does not match what is defined in your jaxws-spring.jar META-INF/spring.schema file. That file should have the following content
http\://jax-ws.dev.java.net/spring/core.xsd=spring-jax-ws-core.xsd
http\://jax-ws.dev.java.net/spring/servlet.xsd=spring-jax-ws-servlet.xsd
http\://jax-ws.dev.java.net/spring/local-transport.xsd=spring-jax-ws-local-transport.xsd
Spring uses this mapping to search the schema in the classpath rather than to internet. Those schema files are located at the root of the jaxws-spring.jar file.
Please take a look Registering the handler and the schema
are you using spring in your project?, I had the same problem, but when I included spring-jaxws in my maven dependencies the problem solved, in fact, yo can see in that jar that the META-INF/spring.schemas file is redefining the XSDs locations
Resources have moved, final config should be:
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.java.net/spring/servlet.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-lazy-init="true">
Probably not a case with yours. But in such a scenario, do clean and make a fresh build. Reason being the older spring jar and xsd from there would be taken if the two jars exits. Hope that helps for other guys .... generically this exception will be thrown for many reasons.

Getting error message with spring "cvc-elt.1: Cannot find the declaration of element 'beans'."

I'm trying to set up a simple spring application and I'm getting the below exception. This is being run standalone in eclipse indigo.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Line 2 in XML document from class path resource [context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
Here's the initial portion of my code:
public static void main(String[] args) {
try {
BeanFactory beanfactory = new ClassPathXmlApplicationContext(
"context.xml");
FirstBean bean = (FirstBean) beanfactory.getBean("show");
Here's my context.xml file:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="anotherBean" class="AnotherBean" />
<bean id="show" class="FirstBean">
<constructor-arg ref="anotherBean" />
</bean>
<bean id="populateFD" class="PopulateFactData">
<constructor-arg value="localhost" />
<constructor-arg value="3309" />
</bean>
</beans>
Are you sure you have spring-beans on the classpath?
This error normally means that it can't find a spring.schemas (which is in spring-beans.jar) explaining to it what that namespace means.
Other options are that the Maven Shade plugin has damaged spring.schemas, but that's unlikely to be the case as you haven't mentioned Maven.
Maybe this post can help you:
Cannot find the declaration of element 'beans' in internet offline mode
It seems like being a problem of Schema configuration.
the maven shade plugin seems to replace the spring.schemas file in the jars,so creating one of our own with all the individual spring.schema contents from each of the jar should solve the issue.
When I had this issue in STS I just cleaned the project and it worked.
There could be possibility of mismatch of Spring Bean version and xsd definition.
For Example
beans xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
While in spring version is 3.0.5-Release
Check classpath version and keep them same. Shoul

Categories

Resources