I have Maven project with Spring. I have backend project as jar in the classpath(added as maven dependency). The backend project is with Spring again and has it's own applicationContext.xml. In the frontend project I have applicationContext.xml and I want to access the application context from tha backend like this:
<import resource="classpath:applicationContext.xml" />
but i got:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:applicationContext.xml]
Offending resource: ServletContext resource [/WEB-INF/appContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
Also tried:
<import resource="classpath*:applicationContext.xml" />
and
<import resource="classpath:/applicationContext.xml" />
If I understand correctly, you want the front end to access the applicationContext.xml of the back end project. Then classpath* is the right statement, and you should make sure namespaces of both configuration files are the same.
For example, this case won't work
Front end:
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
Back end:
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
Related
I am getting the following error when I startup Spring web application in Tomcat.
Basically, as the beans.xml file is processed, the following classes are loaded in order.
org.springframework.context.config.ContextNamespaceHandler to process URI http://www.springframework.org/schema/context
org.springframework.beans.factory.xml.UtilNamespaceHandler to process URI http://www.springframework.org/schema/util
org.apache.cxf.jaxws.spring.NamespaceHandler to process URI http://cxf.apache.org/jaxws
org.apache.cxf.transport.http.spring.NamespaceHandler to process URI http://cxf.apache.org/transports/http/configuration
org.springframework.batch.core.configuration.xml.CoreNamespaceHandler to process URI http://www.springframework.org/schema/batch
But when it is loading the above last class for batch URI, the following Exception occurs.
12/28/2017-11:21:47.070 - localhost-startStop-1 - ERROR [org.springframework.web.context.ContextLoader : Line 227]: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [beans.xml]; nested exception is org.springframework.beans.FatalBeanException: Class [org.springframework.batch.core.configuration.xml.CoreNamespaceHandler] for namespace [http://www.springframework.org/schema/batch] does not implement the [org.springframework.beans.factory.xml.NamespaceHandler] interface
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
I looked into the source of all the above classes and found that all of them extend
org.springframework.beans.factory.xml.NamespaceHandlerSupport
which in turn implements
org.springframework.beans.factory.xml.NamespaceHandler
I have checked the jars on classpath and they look fine, containing the above classes and interfaces in respective, expected spring jar files.
Any idea how to resolve this?
Similar questions are here and I have tried whats given there.
Unexpected exception parsing XML document from class path resource [config/FaceBookSimulator.xml];
and
Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/batch]
Update
I restarted Tomcat with -verbose:class option to print all classes being loaded form whichever jar files. And I found the following:
12/28/2017-15:01:18.251 - localhost-startStop-1 - DEBUG [org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver : Line 156]: Loaded NamespaceHandler mappings:{
http://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler,
http://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler,
http://cxf.apache.org/transports/http-jetty/configuration=org.apache.cxf.transport.http_jetty.spring.NamespaceHandler,
http://www.w3.org/2006/07/ws-policy=org.apache.cxf.ws.policy.spring.PolicyNamespaceHandler,
http://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler,
http://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler,
http://cxf.apache.org/jaxws=org.apache.cxf.jaxws.spring.NamespaceHandler,
http://cxf.apache.org/policy=org.apache.cxf.ws.policy.spring.NamespaceHandler,
http://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler,
http://cxf.apache.org/transports/jms=org.apache.cxf.transport.jms.spring.NamespaceHandler,
http://cxf.apache.org/transports/http/configuration=org.apache.cxf.transport.http.spring.NamespaceHandler,
http://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler,
http://cxf.apache.org/bindings/object=org.apache.cxf.binding.object.spring.NamespaceHandler,
http://cxf.apache.org/ws/addressing=org.apache.cxf.ws.addressing.spring.NamespaceHandler,
http://cxf.apache.org/clustering=org.apache.cxf.clustering.spring.NamespaceHandler,
http://cxf.apache.org/simple=org.apache.cxf.frontend.spring.NamespaceHandler,
http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler,
http://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler,
http://cxf.apache.org/core=org.apache.cxf.bus.spring.NamespaceHandler,
http://cxf.apache.org/jaxrs=org.apache.cxf.jaxrs.spring.NamespaceHandler,
http://cxf.apache.org/binding/coloc=org.apache.cxf.binding.coloc.spring.NamespaceHandler,
http://schemas.xmlsoap.org/ws/2004/09/policy=org.apache.cxf.ws.policy.spring.PolicyNamespaceHandler,
http://www.springframework.org/schema/jms=org.springframework.jms.config.JmsNamespaceHandler,
http://www.springframework.org/schema/batch=org.springframework.batch.core.configuration.xml.CoreNamespaceHandler,
http://www.w3.org/ns/ws-policy=org.apache.cxf.ws.policy.spring.PolicyNamespaceHandler,
http://cxf.apache.org/ws/rm/manager=org.apache.cxf.ws.rm.spring.NamespaceHandler,
http://cxf.apache.org/bindings/soap=org.apache.cxf.binding.soap.spring.NamespaceHandler
}
The class
org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver
is processing:
http://www.springframework.org/schema/batch=org.springframework.batch.core.configuration.xml.CoreNamespaceHandler
The DefaultNamespaceHandlerResolver does not extend NamespaceHandlerSupport ( which implements NamespaceHandler).
If the finding is right, how to resolve this?
My application was relying on shared jar files on the server lib directory. There are many jars over. I needed to sort out the service side jar files. But the quickest solution was to package the needed jars within the WEB-INF/lib of the WAR file.
The whole day I am working on that problem.
I have a workspace with this structur:
cmn-lib (common basic algorithms) # Java
cmn-server (common server based logic) # Java
cmn-dao (database interface) # Java
qz-tomcat (tomcat project) # Java
qz-client (client) # Android
cmn-server as well as cmn-dao using Spring(The tests runs without problems).
the spring configuration of cmn-server-spring.xml includes the common-dao-spring.xml(Becouse some Handler classes needs Dao support).
This is the cmn-server-spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<import resource="cmn-dao-spring.xml" />
<bean id="scoreHandler" class="de.bc.qz.handler.score.ScoreHandler"
autowire="byName">
</bean>
</beans>
now I want to include all those librarys into qz-tomcat.
The problem is that exception:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [cmn-dao-spring.xml]
Offending resource: URL [jar:file:/C:/Users/BC/qz/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/quiz-tomcat/WEB-INF/lib/cmn-server.jar!/cmn-serv-spring.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [jar:file:/C:/Users/BC/qz/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/quiz-tomcat/WEB-INF/lib/cmn-server.jar!/cmn-dao-spring.xml]; nested exception is java.io.FileNotFoundException: JAR entry cmn-dao-spring.xml not found in C:\Users\BC\qz\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\quiz-tomcat\WEB-INF\lib\cmn-server.jar
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:76)
It occurs when I start my local tomcat.
cmn-server and cmn-dao are included as JAR with help of "Web Deployment Assembly".
However... My webapp brokes during SpringBeanAutowiringSupport:
#WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
#Autowired
ScoreHandler mScoreHandler;
#Autowired
TransferAdapter mTransferAdapter;
ScoreCreator mScoreCreator;
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
Is something wrong in my cmn-server.jar?
I think the main problem is that line in the exception:
IOException parsing XML document from URL [jar:file:/C:/Users/BC/qz/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/quiz-tomcat/WEB-INF/lib/cmn-server.jar!/cmn-dao-spring.xml
There is no common-dao-spring.xml in my cmn-server.jar. I have added the project cmn-dao to cmn-server via Java Build Path->Project->Add->cmn-dao
That configuration seems to be fine for JUnit tests but not for the deployed Jar-File.
Knows anybody how to fix that problem.
Thanks for each help.
You should keep your Spring configuration files in the jars they belong to, and it's not a bad idea to use the standard META-INF/spring location. What you want to do is tell Spring to look for the configuration file on the classpath:
<import resource="classpath:[/META-INF/spring]/cmn-dao.xml" />
Also, note that you apparently have a mismatch in your naming, which may be the only problem actually breaking your runtime: You constantly switch back and forth between cmn and common and having or not having spring at the end. Pick one convention and use it.
SEVERE: Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [spring.test.StockValueFetcher] for bean with name 'stockBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: spring.test.StockValueFetcher
and my applicationContext.xml is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="stockBean" class="spring.test.StockValueFetcher">
</bean>
</beans>
i have searched for the same in stackoverflow and other sites but i couldnot find any helpful solution
2 possible causes
Your class isn't called StockValueFetcher or isn't declared in the package spring.test
Your class is not properly deployed and/or defined in the webserver application
It is easy to validate 1.
In case it is 1: correct the spring config file
In case it is 2: check your build & deploy procedure to correctly create the proper web application
I can think of below two possibilities:
Check your build/classes folder. Does it have the class file for "StockValueFetcher" in the appropriate package.
If you are using MAVEN, have you added "Maven Dependencies" to the project's "Web Deployment Assembly". If not, add that as follows (assuming you are using Eclipse):
Right Click on your project -> Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Next and then from there you can add "maven Dependencies". Then build and try to run your app.
I am new to Spring and inherited a Spring project that had all the XML configuration in ProjectName/WebContent/WEB-INF/applicationContext.xml. I'm trying to break the configuration into different components so it is easier to substitute things like DataSources and Hibernate configuation when testing.
Here is my file structure:
ProjectName
->WebContent
->WEB-INF
->applicationContext.xml
->spring-datasource.xml
->spring-hibernate-properties.xml
->spring-persistence.xml
->test
->us.mn.k12... (Java pkgs with JUnit tests)
->spring-hsqldb-datasource.xml
->spring-test-bean-locations.xml
->spring-test-hibernate-properties.xml
->src
->us.mn.k12... (Java pkgs with production code)
In WEB-INF/applicationContext.xml, I import the following:
<import resource="spring-datasource.xml"/> <!-- Production datasource -->
<import resource="spring-hibernate-properties.xml"/> <!-- Production hibernate properties -->
<import resource="spring-persistence.xml"/> <!-- DAO's, hibernate .hbm.xml mapping files -->
The application works with the above configuration.
My JUnit tests run using DbUnit and an HSQLDB in-memory database. So my JUnit test references spring-test-bean-locations.xml, which has the following:
<import resource="spring-hsqldb-datasource.xml"/> <!-- HSQLDB datasource for test -->
<import resource="../WebContent/WEB-INF/spring-persistence.xml"/> <!-- Production DAO's, hibernate .hbm.xml mapping files -->
<import resource="spring-test-hibernate-properties.xml"/> <!-- Hibernate properties for test -->
In this way, I can specify test datasource and hibernate properties, but reuse the production mapping file for the DAO's, etc. However, I get an error running my JUnit test. Here is the relevant part of the exception:
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../WebContent/WEB-INF/spring-persistence.xml]
Offending resource: class path resource [spring-test-bean-locations.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [../WebContent/WEB-INF/spring-persistence.xml]; nested exception is java.io.FileNotFoundException: class path resource [../WebContent/WEB-INF/spring-persistence.xml] cannot be opened because it does not exist
Now if I move spring-persistence.xml into /test so that I don't have to use the relative path, and reference it with <import resource="spring-persistence.xml"/>, then the tests run fine. So I think the contents of my XML files are OK, but I'm not properly importing with a relative path.
Is there anything obvious I'm doing wrong with my import of the relative path? And maybe the bigger question is does this look like a reasonable strategy for breaking applicationContext.xml into components to make it easier for testing?
Thanks!
The problem is: anything inside WEB-INF is not available to the ClassLoader in a regular project setup (and spring uses the ClassLoader by default to access resources). There are some hacks to work around this (like referencing the contexts using the file: prefix), but those are mostly ugly.
A better practice I'd suggest is to move the context files out of WEB-INF and into a dedicated resource directory (src/main/resources if you have a maven setup). That way they will be available to both the webapp ClassLoader and local unit test ClassLoaders.
Read the resources chapter to further understand the mechanisms involved.
Use
<import resource="file:**/WebContent/WEB-INF/spring-persistence.xml" />
It works in spring 3.2.1.RELEASE. Old versions I am not sure.
I have a new web app that is packaged as a WAR as part of a multi-module Maven project. The applicationContext.xml for this WAR references beans that are imported from the "service" module, which in turn imports beans from the "dao" module. The import statement in applicationContext.xml looks like this:
<import resource="classpath*:service.xml" />
and the one inside the service.xml file looks like this:
<import resource="classpath*:dao.xml" />
Neither Spring STS, nor Eclipse show any warnings or errors in my bean files. I reference the imported beans all over the place. The Maven build works fine and the DAO integration tests all pass (they use the beans). I don't have any service integration tests yet.
But when I start up the WAR in Jetty I get an error:
Error creating bean with name 'securityService'
Cannot resolve reference to bean 'userDAO' while setting constructor argument
All of the imported bean XML files can be found inside their respective JAR files in the WEB-INF/lib directory. Indeed, the service bean that threw the error is itself defined inside the service.xml file inside the service module's JAR file.
Apparently the service module can't find the bean that it imported from the dao module. Obviously I don't understand something...seems like this should this Just Work?
I enabled DEBUG logging for 'org.springframework' in order to see if I could learn anything. What I found were messages to the effect that the DAO beans had been created, but there was also a message about them having no name or id.
I check the file, and they all did have an id. So what was it? I check the XML namespace and saw:
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
and noticed it was old (I am using Spring 3.0.2) and changed it to:
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Once I changed it, Spring instantly threw half a dozen errors regarding beans that were defined incorrectly (but never used apparently). Once I fixed those errors, everything Just Worked. I've since gone through the entire system checking Spring XML file namespace versions.
Thanks to all for the help. Can't believe I wasted a day on this stupidity!!
The difference between the classpath:thingy.xml and classpath*:thingy.xml notation is that the former uses the standard classpath mechanism to resolve one resource (using ClassLoader.getResource(name)), whereas the latter will use ClassLoader.getResources(name) to retrieve all matching resources on the classpath, a distinction that should be irrelevant in your situation as I guess there is only one dao.xml file on the class path.
I think your problem is different, you are missing a leading slash.
Use this for a single resource
<import resource="classpath:/dao.xml" />
and this for multiple resources
<import resource="classpath*:/dao.xml" />
See
Spring Reference: The classpath*
prefix
Sun JavaDocs: ClassLoader
It should be like
<import resource="classpath:service.xml"/>
Are you having multiple applicationContexts and possibly the parent context is referring to a bean defined in the child context?