java - 如何在 Hazelcast 地图商店中实例化对象(Jdbc 模板)
问题描述
我正在尝试在 mapStore 中自动装配 jdbc 模板。但我得到了空指针异常。
我做了很多例子,但仍然无法解决这个问题..
这是我的主要课程
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);
}
和 TblRepo 接口我已经用@Repository 注释进行了注释..
还有我的地图商店类
@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 为空,因此存储方法本身的操作失败..* 我也尝试在 DataMapStore 类上添加@component
但就我而言,我遇到了这个错误
- "message": "无法调用 "com.example.demo.repo.DataTblRepository.save(String, String)" 因为 "this.dataTableRepo" 为空",
我在许多平台上也看到了同样的问题,但仍然无法解决这个问题。
任何建议都会非常有帮助
解决方案
SpringAware
用于 Hazelcast 分布式对象(参见文档)。
您的MapStore
示例中的 不是分布式对象,而是一个简单的普通对象。它应该由 Spring 自己管理。您应该用@SpringAware
Spring注释替换@Component
注释。
下一个问题是您的地图存储配置使 Hazelcast 负责实例化MapStore
. 如果发生这种情况,您将无法从 Spring 的依赖注入机制中受益。您应该直接设置由 Spring 创建的实例。
替换
SpringAware
为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; }
我还删除了方法static
上的关键字config()
。
请注意,这种使用方式将MapStore
其与“客户端”代码结合在一起。这意味着您需要使用 Hazelcast 嵌入式。有关嵌入式模式与客户端/服务器的更多信息,请查看与拓扑相关的文档。
推荐阅读
- kubernetes - (HA) API-Server 无法访问,一个主服务器宕机
- python - 更新到字典
- node.js - 带有打字稿的uisng multer:“请求”类型上不存在属性“文件”.ts(2339)
- c++ - 如何在 C++17 中应用链接关联性
- python - 在 finally 子句中返回值是否错误
- reactjs - 处理父组件内的子组件事件
- javascript - 节点 JS:文件上传错误 [ERR_STREAM_WRITE_AFTER_END]
- protractor - 如何返回存储在该部分中的值
- docker - docker:来自守护程序的错误响应:系统上启用了 AppArmor,但无法加载 docker-default 配置文件
- c# - Blazor 服务器应用程序、WebAssembly-HostedByDotNetCore 和 WebAssembly 之间的适当澄清?