hazelcast没有从spring bean获取MapStore(autowired依赖项为null)

时间:2015-05-12 20:13:41

标签: spring dependency-injection hazelcast

我正在为持久性实现hazelcast地图商店。但是无法将自动装配的spring bean(下面的DataSource)放入hazelcast mapstore对象中(意味着hazelcast没有从spring bean获取地图存储对象)。我读到了hazelcast支持spring DI。我错过了什么?下面是我的部分地图商店代码

如果我使用get bean(如下面的

)从上下文中获取bean
MySQLStore store = (MySQLStore)context.getBean(MySQLStore.class);

我通过注入数据源依赖项获取mysql存储。所以这应该是Hazelcast从春天不吃豆子的问题。我错过了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>

1 个答案:

答案 0 :(得分:2)

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

我希望它有所帮助。

谢谢