如何在Hazelcast Map商店中实例化对象(Jdbc模板) [英] How to instantiate object(Jdbc template) inside Hazelcast Map store

查看:47
本文介绍了如何在Hazelcast Map商店中实例化对象(Jdbc模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动连接mapStore中的jdbc模板.但是我遇到了空指针异常.

我处理了很多示例,但仍然无法解决此问题.

I worked on so many examples but sill not able to resolve this issue..

这是我的主班


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestCacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestCacheApplication.class, args);
        System.err.println("......running successfully......");
    }

}

这是我的缓存配置代码

@Component
public class CacheConfig {

    @Bean
    public static Config config() {

        System.err.println("config class");
        Config config = new Config();
        config.setInstanceName("hazelcast");
        
        
        MapConfig mapCfg = new MapConfig();
        mapCfg.setName("first-map");
        mapCfg.setBackupCount(2);
        mapCfg.setTimeToLiveSeconds(300);

        MapStoreConfig mapStoreCfg = new MapStoreConfig();
        mapStoreCfg.setClassName(DataMapStore .class.getName()).setEnabled(true);
        mapCfg.setMapStoreConfig(mapStoreCfg);
        config.addMapConfig(mapCfg);

        return config;

    }

}

和TblRepo实施

@Service
public class DataTblRepoImpl implements DataTblRepo {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(String id, String name) {

        Object[] params = new Object[] { id, name };
        int[] types = new int[] { Types.VARCHAR, Types.VARCHAR };
        String insertSql = "INSERT INTO public.person(id, name)  VALUES(?, ?)";
        jdbcTemplate.update(insertSql, params, types);
    }

我已经用 @Repository 注释对

和TblRepo接口进行了注释.

和我的地图商店类

@SpringAware
public class DataMapStore implements MapStore<String, ModelClass>{

    @Autowired
    DataTblRepo dataTblRepo;

    @Override
    public void store(String key, ModelClass value) {
        dataTblRepo.save(value.getId(), value.getName());

    }
//remaining methods will come here
}

和控制器

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/v1")
public class DataController {

    @Autowired
    DataService dataService;

    HazelcastInstance hazelCast = Hazelcast.getHazelcastInstanceByName("hazelcast");

    @PostMapping("/{test}")
    public String saveDatafrom(@RequestBody ModelClass model) {

        hazelCast.getMap("first-map").put(model.getId(), model);
        return "stored";
    }

}

这是程序流程..当我启动应用程序时,将运行第一个Cacheconfig类.

  • 在控制器中,当我执行map.put()操作时,数据将转到DataMapStore类并调用store方法以将数据保存在数据库中.因为DataTblRepo为null,所以操作在store方法本身上失败了.. *我也尝试在DataMapStore类上添加@component

但是在我的情况下,我遇到了这个错误

  • 消息":无法调用"com.example.demo.repo.DataTblRepository.save(String,String)"因为"this.dataTableRepo"为空",

我在许多平台上也看到了相同的问题,但仍然无法解决此问题.

任何建议都会很有帮助

推荐答案

SpringAware 用于Hazelcast分布式对象(请参阅

SpringAware is for Hazelcast distributed objects (cf. documentation).

示例中的 MapStore 不是分布式对象,而是简单的普通对象.它应该由Spring自己管理.您应该用Spring @Component 注释替换 @SpringAware 注释.

The MapStore in your example is not a distributed object but a simple plain object. It should be managed by Spring itself. You should replace the @SpringAware annotation by a Spring @Component annotation.

下一个问题是您的地图存储配置使Hazelcast负责实例化 MapStore .如果发生这种情况,您将无法从Spring的依赖注入机制中受益.您应该直接设置由Spring创建的实例.

The next issue is that your map store configuration makes Hazelcast responsible to instantiate the MapStore. If this happens, you won't benefit from Spring's Dependency Injection mechanism. You should directly set the instance created by Spring.

  1. Component

@Component
public class DataMapStore implements MapStore<String, ModelClass> {
    // ...
}

  • 使用Spring配置的 MapStore 实例

    @Bean
    public Config config(DataMapStore mapStore) { // Ask Spring to inject the instance
    
        // ...
    
        MapStoreConfig mapStoreCfg = new MapStoreConfig();
        mapStoreCfg.setImplementation(mapStore);  // Use it
        mapCfg.setMapStoreConfig(mapStoreCfg);
        config.addMapConfig(mapCfg);
    
        return config;
    }
    

  • 我还删除了 config()方法上的 static 关键字.

    I also removed the static keyword on the config() method.

    请注意,这种使用 MapStore 的方式会将其与客户端"耦合.代码.这意味着您需要使用嵌入式的Hazelcast.有关嵌入式模式与客户端/服务器的更多信息,请检查

    Note that this way of using MapStore couples it with the "client" code. This means you need to use Hazelcast embedded. For more information about embedded mode vs. client/server, please check the documentation related to topology.

    这篇关于如何在Hazelcast Map商店中实例化对象(Jdbc模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆