我有一个grails项目,我想在其中配置Hazelcast。但我无法做到。我已经下载了hazelcast并添加到了lib文件夹中。然后我尝试在资源文件中初始化
hazelcastConfigurer(MethodInvokingFactoryBean){
targetClass = "com.hazelcast.core.Hazelcast"
targetMethod = "init"
Config hazelcastConfig = new Config("hazelcast.xml")
arguments = hazelcastConfig
}
它不会编译并抛出错误
[2014-06-04 22:08:25,293] - context.GrailsContextLoader Error initializing the a
pplication: Error creating bean with name 'hazelcastConfigurer': Invocation of i
nit method failed; nested exception is java.lang.NoSuchMethodException: com.haze
lcast.core.Hazelcast.init(com.hazelcast.config.Config)
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'hazelcastConfigurer': Invocation of init method failed; nested exception
is java.lang.NoSuchMethodException: com.hazelcast.core.Hazelcast.init(com.hazel
cast.config.Config)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NoSuchMethodException: com.hazelcast.core.Hazelcast.init(co
m.hazelcast.config.Config)
at java.lang.Class.getMethod(Class.java:1624)
... 5 more
是否有任何博客或网站可以帮助我在grails项目中进行配置?
答案 0 :(得分:1)
如果您使用的是Hazelcast 3.0版,则应该是targetMethod = "newHazelcastInstance"
而不是targetMethod = "init"
。
答案 1 :(得分:1)
以下是我在Grails 2.1.2应用程序中配置Hazelcast 3.2以使用Spring缓存注释的方法:
BuildConfig.groovy
dependencies {
...
compile "com.hazelcast:hazelcast:3.2"
compile "com.hazelcast:hazelcast-spring:3.2"
}
Hazelcast实例配置,src/java/spring/hazelcast.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:hz="http://www.hazelcast.com/schema/spring"
xsi:schemaLocation="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.hazelcast.com/schema/spring
http://www.hazelcast.com/schema/spring/hazelcast-spring-3.2.xsd">
<hz:hazelcast id="hazelcastInstance">
<hz:config>
<hz:map name="myCache"
backup-count="0"
in-memory-format="OBJECT"
>
</hz:map>
</hz:config>
</hz:hazelcast>
</beans>
启用Spring缓存注释src/java/spring/spring-cache.xml
:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven/>
</beans>
将所有内容连接在一起 - Spring缓存注释使用的缓存管理器bean使用Hazelcast实例bean grails-app/conf/spring/resources.groovy
进行配置:
beans = {
importBeans('classpath:/spring/spring-cache.xml')
importBeans('classpath:/spring/hazelcast.xml')
cacheManager(com.hazelcast.spring.cache.HazelcastCacheManager, ref('hazelcastInstance'))
}
现在,您可以将@Cacheable
和@CacheEvict
等Spring缓存注释与Hazelcast缓存一起使用:
@Cacheable(value = 'myCache')
SomethingDto getSomethingDto(Long id) {
SomethingDto dto = convertToSomethingDto(Something.get(id))
return dto
}
我通常将域对象转换为DTO,因为将普通域对象存储到缓存中会导致问题,如果它的一些属性被延迟提取。
很少注意到:
src/java
,因为grails-app/conf/spring
在生产中找不到它们,请参阅Load spring beans from custom groovy files in grails app resources.groovy
中使用环境块实现每个环境Hazelcast配置。我使用它,但为了简单起见,我没有在这里发布。