gpt4 book ai didi

xml - 在Grails项目中配置Hazelcast

转载 作者:行者123 更新时间:2023-12-02 14:24:00 25 4
gpt4 key购买 nike

我有一个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项目中进行配置?

最佳答案

这是我在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>

启用S​​pring缓存注释 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'))
}

现在,您可以将Spring缓存注释(如 @Cacheable@CacheEvict)与Hazelcast缓存一起使用:
    @Cacheable(value = 'myCache')
SomethingDto getSomethingDto(Long id) {
SomethingDto dto = convertToSomethingDto(Something.get(id))
return dto
}

我通常将域对象转换为DTO,因为如果懒惰地提取普通域对象的某些属性,则将其存储到缓存中可能会引起问题。

一些注意事项:
  • 我将Spring XML文件放入src/java中,因为在grails-app/conf/spring中在生产中找不到它们,请参见Load spring beans from custom groovy files in grails app
  • 您可以使用resources.groovy中的环境块来实现每个环境的Hazelcast配置。我使用了它,但是为了简单起见,我没有在这里发布它。
  • 关于xml - 在Grails项目中配置Hazelcast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24042892/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com