Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring - How to Configure Eclipse to Work with `ehcache`

Summary/Question

I've rolled onto a project that uses ehcache. The project is maven enabled and when I run mvn clean install from cli all ends well.

The project can also be opened in NetBeans and everything displays properly, however when I open the project in eclipse, I'm seeing some errors related to ehcache. In NetBeans the spring-cache.xml is located under a "Web Pages" folder. This made me think that the eclipse project may not be recognized as a dynamic web project, however following the directions here: https://www.mkyong.com/java/how-to-convert-java-project-to-web-project-in-eclipse/ I was able to verify that the project is indeed setup to support Dynamic Web Module, Java and JavaScript.

I also opened the project in IntelliJ and also did not see any issues with the spring-cache.xml.

I also didn't find documentation related to this version of ehcache on http://www.ehcache.org/documentation/ but the fact that things work in NetBeans, IntelliJ and via mvn clean install make me think its my eclipse setup. Thoughts?

More Details:

The errors I see in eclipse are:

Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'ehcache:annotation-driven'.
- schema_reference.4: Failed to read schema document 'http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring-1.1.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>.

enter image description here

And

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'ehcache:config'.

enter image description here

My maven dependency looks like:

<dependency>
    <groupId>com.googlecode.ehcache-spring-annotations</groupId>
    <artifactId>ehcache-spring-annotations</artifactId>
    <version>1.2.0</version>
</dependency>

Which matches what it should be, based on: https://mvnrepository.com/artifact/com.googlecode.ehcache-spring-annotations/ehcache-spring-annotations/1.2.0

My spring-cache.xml file looks like:

<?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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
                http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

    <ehcache:annotation-driven />
    <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements interval="60" />
    </ehcache:config>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation"  value="${ehcache.config.location}"/>
    </bean>

</beans>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like current versions of NetBeans and IntelliJ aren't as strict on checking things as eclipse is. :) The following is what I found out.

From ehcache with Spring. google code xsd file not found I found out that the top error was related to the fact that the xsd is no longer hosted at the url provided.

Doing some googles, I found a reference to the version on github at: https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.2.xsd by changing my bottom xsi schemalocation to the above url, it resolved the issue in eclipse.

The stack overflow questions referenced, also provides a link to the google code archive of: https://code.google.com/archive/p/ehcache-spring-annotations/source/default/source

If you download the zip, you can then find the offical xsd in the zip at: /schema/ehcache-spring/ehcache-spring-1.2.xsd (it also has ehcache-spring-1.0.xsd and ehcache-spring1.1.xsd in case anyone else has issues with even earlier versions).

I found a way to reference the local schema file here: How to reference a local XML Schema file correctly?

I'm going to post a question on how to reference it from a relative path of the project, as we have both windows and mac developers on this project. If this is of use to you, the details will be here: How to Reference Local XSD File By Relative Path


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...