springMVC+mybatis+ehcache詳細配置

如何在Spring MVC中使用Ehcache?作爲 Java 開發人員都想要知道,不是嗎?基於Spring註解的ehcache是 Apache 許可證許可下的一個項目,使用Ehcache庫簡化了基於Spring 應用程序中的緩存。1.1.2 版的 ehcache,Spring註解已只是最近才被釋放。在這篇文章中我將介紹如何創建一個 web 項目。

創建一個新的 web 項目,將使用基於SpringMVC的web項目。如果您使用 Eclipse 請確保你有 m2eclipse 安裝,然後創建一個Maven項目使用 JEE 5 web 應用程序原型(組 id: org.codehaus.mojo.archetypes、 工件 id: webapp jee5);

上述描述需要Eclipse 3.6完美運行。

首先我們在項目中創建一個MessageController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@Controller
public class MessageController {
  
    @Autowired(required = true)
    private MessageStorage messageStorage;
  
    public MessageController(MessageStorage messageStorage) {
        super();
        this.messageStorage = messageStorage;
    }
  
    public MessageController() {
  
    }
  
    @RequestMapping(method = RequestMethod.GET, value = "/message/add")
    public ModelAndView messageForm() {
        return new ModelAndView("message-form""command"new Message());
    }
  
    @RequestMapping(method = RequestMethod.POST, value = "/message/add")
    public ModelAndView addMessage(@ModelAttribute Message message) {
        messageStorage.addMessage(message);
        return getMessageById(message.getId());
    }
  
    @RequestMapping(method = RequestMethod.GET, value = "/message/{id}")
    public ModelAndView getMessageById(@PathVariable("id"long id) {
        Message message = messageStorage.findMessage(id);
        ModelAndView mav = new ModelAndView("message-details");
        mav.addObject("message", message);
        return mav;
    }
  
    @RequestMapping(method = RequestMethod.GET, value = "/message")
    public ModelAndView getAllMessages() {
        Collection<Message> messages = messageStorage.findAllMessages();
        ModelAndView mav = new ModelAndView("messages");
        mav.addObject("messages"new CollectionOfElements(messages));
        return mav;
    }
}

下面我們創建DAO接口

1
2
3
4
5
public interface MessageStorage {
    Message findMessage(long id);
    Collection<Message> findAllMessages();
    void addMessage(Message message);
}

DAO的實現類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Component
public class MemoryMessageStorage implements MessageStorage {
  
    private Map<Long, Message> messages;
  
    private AtomicLong newID;
  
    public MemoryMessageStorage() {
        // ...
  
        // initialize some messages
        addMessage(new Message("user:1""content-1"));
        addMessage(new Message("user:2""content-2"));
        addMessage(new Message("user:3""content-3"));
        addMessage(new Message("user:4""content-4"));
        addMessage(new Message("user:5""content-5"));
    }
  
    @Override
    public Message findMessage(long id) {
        // ...
    }
  
    @Override
    public Collection<Message> findAllMessages() {
        // ...
    }
  
    @Override
    public void addMessage(Message message) {
        // ...
    }
}

MAVEN配置Spring需要jar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.3.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>3.0.3.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
</dependencies>

在SpringMVC項目中引入Ehcache註解

1
2
3
4
5
6
7
<dependency>
    <groupId>com.googlecode.ehcache-spring-annotations</groupId>
    <artifactId>ehcache-spring-annotations</artifactId>
    <version>1.1.2</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

如果我們使用Spring Ehcache 註解版本是 2.1.0。我還需要添加了SLF API實現

1
2
3
4
5
6
7
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.1</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

與上面的依賴性,我們可以使用基於註解的Spring ehcache 。正如前面提到,我們會向 MemoryMessageStorage 添加註解。有一些簡單的規則:

使用緩存名稱"messageCache"的 findMessage(long) 調用緩存消息

使用緩存名稱"messagesCache"的 findAllMessages() 調用緩存消息

addMessage(Message) 在調用後從"messagesCache"中移除所有元素


要實現上述目標,我們使用下面的 @Cachable 和 @TriggersRemove 註解 aspresented:

Spring和Ehcache的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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"/>
  
    <!-- rest of the file omitted -->
  
</beans>

在WEB-INF下創建ehcache.xml

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
    <cache name="messageCache" maxElementsInMemory="10" eternal="true" overflowToDisk="false" />
    <cache name="messagesCache" maxElementsInMemory="10" eternal="true" overflowToDisk="false" />
</ehcache>

然後需要在Spring的配置文件中加入

1
2
3
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation"  value="/WEB-INF/ehcache.xml"/>
</bean>

啓動tomcat就OK了,如果大家需要Unit testing,可一在網上找一下

發佈了81 篇原創文章 · 獲贊 91 · 訪問量 40萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章