如何在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,可一在网上找一下