I am noticing a strange issue with respect to spring XML schemas.
I have a standalone java application which uses spring framework. As long as I run this application within eclipse, I do not face any issues. However, when I package this as a jar file (as described in this link), and execute the jar, I get the following exception:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [applicationContext.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:316)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1416)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1409)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
I have the following entry in applicationContext.xml and it works fine inside eclipse:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
Any help is much appreciated.
I even tried changing http://www.springframework.org/schema/tx/spring-tx-3.1.xsd to classpath:/org/springframework/transaction/config/spring-tx-3.1.xsd but it did not help.
It looks like your application contains some jars, like spring-core-3.1.x (because its classes are being used), but it's missing spring-tx-3.1.x.RELEASE.jar (the one that holds Spring Transaction classes).
Related
I have two modules in my project which are children of the main app.pom
<modules>
<module>commons</module>
<module>webapp</module>
</modules>
The webapp module refers to commons this way:
<dependency>
<groupId>com.app.whatever</groupId>
<artifactId>commons</artifactId>
<version>${project.version}</version>
</dependency>
The commons module is just packaged in .jar file and that's it. All their dependencies webapp and commons inherit from app.pom.
Both commons and webapp use Spring 3 with .xml files that describe application contexts.
In webapp module I want to use a bean from commons module. Here's applicationContext for commons:
<?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"
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.xsd">
<context:component-scan base-package="com.app.whatever.anything"/>
</beans>
For webapp applicationContext.xml I have:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<import resource="classpath*:/applicationContext-commons.xml"/>
<context:component-scan base-package="com.app.whatever.something.controller.spring"/>
But unfortunately this doesn't work. Constantly get an exception about no candidates for autowiring, though I can use any classes from the second module in the first one after setting the above maven dependency.
When I write this import without asterisk, it says that the file doesn't exist.
I tried without leading slash - the same exception.
This import works perfectly for any file from the same module and even for external libraries I have. But it can't resolve the file from this new commons module I've just created.
If you have any idea, that'd be really helpful, thanks
If it's not under a subdirectory, e.g. spring/applicationContext-commons.xml then the following (which you've already tried) should work:
<import resource="classpath*:applicationContext-commons.xml"/>
With an asterisk, Spring will use all matching files on the classpath, without the asterisk it will use the first one it finds.
Also, with the asterisk Spring will ignore the file if it wasn't found. Without the asterisk Spring will give an error if it can't find the file. Therefore your file is not on the classpath.
I'd check the location of applicationContext-commons.xml under the target folder to make sure <import resource= has the correct path and re-run mvn install from the root pom.
I have 3 modules on project. One of them calls common. In this module i am trying to start Spring container. One of beans live in another module (webapp) and also i have a module testapp. So now, after deploying project on server (webapp and common) it is all OK, but if I try to deploy common and testapp it fails with exception:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class[...]
i whant to know if it is any possibility to ignore unexisting bean and run container? I am using xml configuration, no annotations. Due to business logic it should be exactly xml
Make use of Spring profiles to determine when specific beans should be instantiated. For example you can have two profiles web (for common + webapp) and test (for common + testapp)
Then in your application context xml configuration define your beans as follows
<?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"
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-4.2.xsd">
<!-- define common beans here-->
<beans profile="test">
<!-- define test beans here-->
</beans>
<beans profile="web">
<!-- define web beans here-->
</beans>
</beans>
Then depending on how you launch your application you need to provide a system property named spring.profiles.active to provide the profile to use.
For example -Dspring.profiles.active=web or -Dspring.profiles.active=test
I'm developing an app with Java and Spring and I'm trying to upload it to AppFog.
The application is working properly and it's working in AppFog, but in Eclipse I'm getting a very annoying error in my applicationContext.xml file.
According to the documentation in AppFog I have to set it up like this: Spring - AppFog Documentation
Snippet of my applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cloud="http://schema.cloudfoundry.org/spring"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://schema.cloudfoundry.org/spring
http://schema.cloudfoundry.org/spring/cloudfoundry-spring.xsd">
<cloud:data-source id="dataSource">
In the tag cloud:data source I'm getting the error:
Multiple annotations found at this line:
- schema_reference.4: Failed to read schema document 'http://schema.cloudfoundry.org/spring/cloudfoundry-spring.xsd',
because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'cloud:data-source'.
I've tried with http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.8.xsd and different versions but I'm always getting the same error.
I have included in my pom the next library too:
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-runtime</artifactId>
<version>0.8.1</version>
</dependency>
Some ideas?
Thanks!
So it looks like your app is working locally and on AppFog, but as you say the issue is a validation error in Eclipse. Are you using the Spring Tool Suite plugin? What should actually happen here is that the schema should be found within the Spring Jar file (the source is on Github https://github.com/cloudfoundry/vcap-java/blob/master/cloudfoundry-runtime/src/main/resources/META-INF/spring.schemas) - I guess there should be a way of telling Eclipse to resolve it there, and I suspect that STS would actually know how to resolve it.
I'm trying to run a system left by an ex-employee here in my job, but I'm having problems to do it.
If the XSD runs by remote access:
<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"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
It gives an not found exception:
Failed to read schema document 'http://www.springframework.org/schema/context/spring-context-3.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
And if the XSD runs by local access:
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
classpath:org/springframework/transaction/config/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
classpath:org/springframework/context/config/spring-context-3.0.xsd
http://www.springframework.org/schema/tool
classpath:org/springframework/beans/factory/xml/spring-tool-3.0.xsd">
It gives this exception:
C:\Users\claudiomazur>java -jar c:\temp\fin\c.jar
0 [AWT-EventQueue-0] INFO support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#4fa52fdf: startup date [Thu Sep 06
11:22:59 BRT 2012]; root of context hierarchy
45 [AWT-EventQueue-0] INFO xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [context.xml]
Exception in thread "AWT-EventQueue-0" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for
XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource [context.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
Some idea to solve this problem?
Big hug!!
The Spring XDSs are defined and shipped with the Spring jars! For Example the spring-context.jar contains the spring-context-3.0.xsd. (see spring-context.jar/META-INF/spring.schema)
So check that you have this jar in your project.
So what you called "remote" is not really remote! And I have never see what you called "local access", so I would try to check why the "remote" (that is not remote) stuff works for all xsds but the spring-context.
If it is a desktop program and you use a single jar that contains all the other exploded jars, then you need a to take care that the content of the single spring.handlers and spring.schames files from the different spring-xxx.jar is COMBINED in (two) "hunge" files. So that in the end this (two) files contains all the schema names and handler names.
Its true that the SPRING XSD's are shipped along with the jars.
The information where these XSD's are stored :
Lets say in the XML file, we have used beans namespace and we need to look into
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
Please check the file "spring-schemas" spring-beans-4.0.1.RELEASE.jar in META-INF folder.
Sample context from this file should be:
http\://www.springframework.org/schema/beans/spring-beans-4.0.xsd
=org/springframework/beans/factory/xml/spring-beans-4.0.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd
=org/springframework/beans/factory/xml/spring-beans-4.0.xsd
Thus
http\://www.springframework.org/schema/beans/spring-beans.xsd is mapped to
org/springframework/beans/factory/xml/spring-beans-4.0.xsd
in your jar
try run wget http://www.springframework.org/schema/context/spring-context-3.0.xsd.
should be something like: wget http://www.springframework.org/schema/context/spring-context-3.0.xsd
--2012-09-10 15:56:05-- http://www.springframework.org/schema/context/spring-context-3.0.xsd
Resolving www.springframework.org... 205.140.197.88
Connecting to www.springframework.org|205.140.197.88|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19651 (19K) [text/xml]
Saving to: “spring-context-3.0.xsd”
100%[=====================================================================>] 19,651 24.6K/s in 0.8s
2012-09-10 15:56:06 (24.6 KB/s) - “spring-context-3.0.xsd” saved [19651/19651]
If you have a problem with getting xsd schema, it makes difficult to work with xml files inherited this schema.
In second case you simply don't have in classpath any of spring-*-3.0.xsd files.
I tried everything I can think of, but nothing seems to work.
I am using the current version of Spring Framework (3.2) in my java web application.
Every time I start my project I get the following error:
cvc-elt.1: Cannot find the declaration of element 'beans'
this is my applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
</beans>
I tried to :
change the version of the schema (3.2, 2.0...)
copy the schema from the jar into WEB-INF
change unix EOL into windows EOL
and nothing seems to work, except using the DTD declaretion instead of the XSD.
What should I do?
Spring is having trouble finding the xsds. Take a look at this SO post. If you are sure you have the required spring-beans jar in your classpath, you may have a corrupted META-INF/spring.schemas file. The spring.schemas file tells spring which classpath to use to find the corresponding xsd file in the spring jars. I have had this occur when using the maven-shade plugin improperly configured.
The xsd came down fine for me when I pasted it in the browser.
This one works fine for me:
<?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"
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">
<context:annotation-config/>
<context:component-scan base-package="example"/>
</beans>