Spring mvc cannot find CacheInterceptor - java

I'm exploring cache in spring mvc framework. Following the guide on internet like here or here, i'm configuring:
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
... other configuration are omitted....
</beans>
Before adding configuration for cache, my web app run normally, now, my web app start with error:
java.lang.NoClassDefFoundError: org/springframework/cache/interceptor/CacheInterceptor
Not sure what i did wrong?

A NoClassDefFoundError is simply an exception that is thrown when java can't find a certain class that another library uses.
All this means is that when it gets thrown you just need to get it on the classpath. You can make sure it's there by going to libraries-> maven dependencies(if you are using maven) -> and then go to the location of the noclassdef that is being thrown, in your case it would be at org/springframework/cache/interceptor/CacheInterceptor, this is just so you know why you are getting this error, you should not see it now.
As for how to solve it, this could be happening because of 3 things:
Your maven dependencies are not on the deployment assemblies(libraries are in the project, but not on the server), although probably spring wouldn't work at all if this was the case.
You don't have the dependency, add this to pom.xml:
org.springframework
spring-context
4.1.4.RELEASE
You have conflicting spring versions, make all version tags the same.

Related

How to import Spring application context from another maven module?

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.

java.net.MalformedURLException: unknown protocol: classpath

I am trying to import the xsd schema from local drive using schemalocation classpath in spring. I added the required file in the classpath and added the reference of this file with
15:10:19.336 [localhost-startStop-1] DEBUG o.s.b.f.xml.ResourceEntityResolver - Could not resolve XML entity [classpath:spring-social-facebook-1.1.xsd] against system root URL
java.net.MalformedURLException: unknown protocol: classpath
at java.net.URL.<init>(Unknown Source) ~[na:1.8.0_31]
at java.net.URL.<init>(Unknown Source) ~[na:1.8.0_31]
at java.net.URL.<init>(Unknown Source) ~[na:1.8.0_31]
here is the header for my applicationContext.xml,
<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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:social="http://www.springframework.org/schema/social"
xmlns:facebook="http://www.springframework.org/schema/social/facebook"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
http://www.springframework.org/schema/social/facebook classpath:spring-social-facebook-1.1.xsd">
I am using spring 4.1 with and spring-bean version 3.0.
You shouldn't be messing around with other locations and use the defaults. For Spring Social Facebook you should use http://www.springframework.org/schema/social/spring-social-facebook-1.1.xsd or preferably the version less one http://www.springframework.org/schema/social/spring-social-facebook.xsd.
Spring will always first load the xsd from the jars from the classpath. Spring ships with a custom EntityResolver, the PluggableSchemaResolver which uses the different spring.schemas files in the META-INF directory of Spring jar files.
The only reasons to get an error like you get (and why you try to work around it) is
You haven't included the spring-social-facebook needed dependencies
You have included the wrong spring-social-facebook dependencies
You are using incompatible Spring and Spring Social versions.
I would bet on option 2 not having all the 1.1.0 versions of spring-social-facebook but mixing versions of jars.
I have resolved the issue, as i was using the wrong jar of spring-social-web but not spring-social-facebook that contain the xsd. I don't required to use the classpath any more. thanks M. Deinum.

Failed to read schema document Schema Cloudfoundry

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.

Spring Configuration Error: (cvc-etl.1)

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>

Validating spring config xml Files in Java

i want to validate some files looking like this:
<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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd />
...
<bean>...</bean>
...
</beans>
Some of them have more xmlns and locations and some less. Someone knows if there is any api where u just give in such files and it will validate it in java?
greetings
Spring will validate spring configuration files. You can write a junit that loads your configuration to test if the configuration files are formatted correctly.
There you go.
http://download.oracle.com/javase/1,5,0/docs/api/javax/xml/validation/package-summary.html
You can wrap it and make parser that will first extract schemas, then use them for validation.

Categories