バージョン 2.1 までに、Hazelcast は Spring コンテキストおよび/または Spring Bean を Hazelcast 管理対象オブジェクトに注入できます。
Hazelcast Spring 構成を使用して Hazelcast を構成し、 を使用して Bean に注釈を付けると@SpringAware
、Hazelcast はその Bean の依存関係を注入するよう Spring に要求します。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hz="http://www.hazelcast.com/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.hazelcast.com/schema/spring
http://www.hazelcast.com/schema/spring/hazelcast-spring-2.1.xsd">
<hz:hazelcast id="instance">
<hz:config>
<hz:group name="dev" password="password"/>
<hz:network port="5701" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="false" />
<hz:tcp-ip enabled="true">
<hz:members>10.10.1.2, 10.10.1.3</hz:members>
</hz:tcp-ip>
</hz:join>
</hz:network>
...
</hz:config>
</hz:hazelcast>
<bean id="someBean" class="com.hazelcast.examples.spring.SomeBean"
scope="singleton" />
...
</beans>
@SpringAware
public class SomeTask implements Callable<Long>, ApplicationContextAware, Serializable {
private transient ApplicationContext context;
private transient SomeBean someBean;
public Long call() throws Exception {
return someBean.value;
}
public void setApplicationContext(final ApplicationContext applicationContext)
throws BeansException {
context = applicationContext;
}
@Autowired
public void setSomeBean(final SomeBean someBean) {
this.someBean = someBean;
}
}
2.1 より古いバージョンの場合:
Hazelcast の 2.1 より前のバージョンは Spring に対応していないため、Spring コンテキストまたは任意の Spring Bean を 2.1 より前のバージョンの Hazelcast 管理対象オブジェクトに注入することはできません。
Hazelcast グループにこの機能について質問する投稿があります。
Hazelcast / Callable の依存性注入
既にご存知かもしれませんが、Hazelcast グループで提案されているように、次を使用して Spring ApplicationContext にアクセスできます。
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context = null;
public synchronized void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
if(context == null) {
context = applicationContext;
}
}
public static <T> T getBean(String name) {
return (T) context.getBean(name);
}
}
class MyCallable implements Callable {
....
public Object call() throws Exception {
SomeServiceBean bean = ApplicationContextProvider.getBean("serviceBean");
....
}
}