Java 如何在Hazelcast映射存储中实例化对象(Jdbc模板)
我正在尝试在mapStore中自动连接jdbc模板。。但我得到了空指针异常 我做了这么多的例子,但无法解决这个问题 这是我的主课Java 如何在Hazelcast映射存储中实例化对象(Jdbc模板),java,hazelcast,Java,Hazelcast,我正在尝试在mapStore中自动连接jdbc模板。。但我得到了空指针异常 我做了这么多的例子,但无法解决这个问题 这是我的主课 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestCacheApplication {
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......");
}
}
这是我的缓存配置代码
以及TblRepo的实施
我用@Repository注释了TblRepo接口
还有我的地图商店课
下面是程序流程。。启动应用程序时,将运行第一个Cacheconfig类
- 在控制器中,当我执行map.put()操作时,数据将转到DataMapStore类并调用store方法将数据保存在数据库中..因为DataTblRepo为null,所以操作在store方法本身失败* 我还尝试在DataMapStore类中添加@component
- “消息”:“无法调用”com.example.demo.repo.DataTblRepository.save(字符串,字符串)”,因为“this.dataTableRepo”为空
任何建议都会非常有用
SpringAware
适用于Hazelcast分布式对象(参见)
示例中的MapStore
不是分布式对象,而是简单的普通对象。它应该由Spring自己管理。您应该将@SpringAware
注释替换为Spring@组件
注释
下一个问题是地图存储配置使Hazelcast负责实例化地图存储
。如果发生这种情况,您将无法从Spring的依赖项注入机制中获益。您应该直接设置Spring创建的实例
组件替换SpringAware
@组件
公共类DataMapStore实现MapStore{
// ...
}
MapStore
实例
@Bean
公共配置配置(DataMapStore-mapStore){//要求Spring注入实例
// ...
MapStoreConfig mapStoreCfg=新的MapStoreConfig();
setImplementation(mapStore);//使用它
setMapStoreConfig(mapStoreCfg);
config.addMapConfig(mapCfg);
返回配置;
}
config()
方法上的static
关键字
请注意,这种使用MapStore
的方法将其与“客户机”代码相结合。这意味着您需要使用Hazelcast embedded。有关嵌入式模式与客户端/服务器的更多信息,请查看
@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;
}
}
@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);
}
@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";
}
}