hazelcast 没有从 spring bean 获取 MapStore(自动连接的依赖项为空)

hazelcast not getting MapStore from spring beans (autowired dependency is null)

我正在实施 hazelcast 地图存储以实现持久性。但是无法将 spring beans(下面的数据源)自动连接到 hazelcast mapstore 对象中(意味着 hazelcast 没有从 spring beans 获取地图存储对象)。我读到 hazelcast 支持 spring DI。我错过了什么?下面是我的部分地图商店代码

如果我使用如下所示的获取 bean 从上下文中获取 bean

MySQLStore store = (MySQLStore)context.getBean(MySQLStore.class);

我得到了 mysql 存储,其中注入了数据源依赖项。所以这应该是 Hazelcast 没有从 spring 获取 bean 的问题。我是否缺少 hazelcast 地图商店中的任何配置。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

import com.hazelcast.core.MapStore;

@Component
public class MySQLStore implements MapStore<String, ScripDetails> {

    @Autowired
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public ScripDetails load(String arg0) {
        System.out.println("loading data from store");

        String sql = "SELECT * FROM DATA";

        Connection conn = null;

下面是 spring-beans xml 具有数据源和组件扫描,其中包括 MySQLStore 包。

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="com.tlab"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://host:port/DB" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

</beans>

Hazelcast xml 低于

<?xml version="1.0" encoding="UTF-8"?>

<hazelcast
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.0.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <network>
        <join><multicast enabled="true"/></join>
    </network>

    <map name="scrips">
        <backup-count>1</backup-count>
        <time-to-live-seconds>0</time-to-live-seconds>
        <max-idle-seconds>0</max-idle-seconds>
        <eviction-policy>LRU</eviction-policy>
        <eviction-percentage>25</eviction-percentage>
        <merge-policy>hz.ADD_NEW_ENTRY</merge-policy>
        <map-store enabled="true">
              <class-name>com.tlab.MySQLStore</class-name>
        </map-store>
    </map>
</hazelcast>

在您的示例中,Hazelcast 实例不是 Spring bean。您需要使用 Spring 配置 Hazelcast 实例。您可以找到文档 here and examples

希望对您有所帮助。

谢谢