Spring 启动 + Hazelcast MapStore 无法自动装配存储库
Spring Boot + Hazelcast MapStore can't Autowire Repository
我开始探索 Hazelcast 功能,并且一直在尝试通过 HazelcastRepository 使用 MapStore 作为我的数据库的后写缓冲区。我的目标是在我的 MapStore 中使用 JpaRepository 来加载和存储 to/from 缓存。
我正在使用 Spring Boot,在做了一些研究之后我发现我可以使用 @SpringAware 在 MapStore 中自动连接我的存储库但是每次它到达那里我的 Bean 是null
我得到一个 NullPointerException
。
即使经过许多不同的测试我也无法让它工作 我无法在 MapStore
中自动装配我的 bean
启用 SpringAware 的配置有问题还是我看错地方了?
找到 ,它给了我线索,但我仍然无法弄清楚问题,因为大多数配置是 xml 而不是 java。
还
找到了关于如何通过 Java 配置 Spring 在 Hazelcast 中配置 SpringAware 的 Github Issue
我在 Git Repo Here 中提交了我的示例代码。
这里的问题是你正在创建名称为 pr
的 spring bean,当你使用 personRepository
.
自动连接时
在PersonRepository.java
中将pr
更改为personRepository
或者从 PersonRepository.java
中删除 ("pr")
@Autowired
PersonRepository personRepository;
DAO
@Repository("pr")
public interface PersonRepository extends JpaRepository<Person, Integer> {
@Query("SELECT id FROM Person")
Iterable<Integer> findAllId();
}
调查提供的代码后,我注意到根据我在 Hazelcast 上发现的 GitHub 问题,默认情况下从未启用 @SpringAware。该问题描述了 SpringAware 被禁用是因为它影响了性能,这将我转发到另一个已关闭的票证,解决了使用 SpringManagedContext 通过代码启用注释的问题(避免使用 XML ),但仍不能解决问题。
找到真正的解决方案 here,将 MapLoaderLifecycleSupport 接口添加到您的 MapStore 实现并实现 init 方法,如票证所示:
@Override
public void init(HazelcastInstance hazelcastInstance, Properties properties, String mapName) {
hazelcastInstance.getConfig().getManagedContext().initialize(this);
}
这将强制在 MapStore class 中启用 @SpringAware,因此,能够将任何 spring 组件自动装配到 class 如下图所示。
working-jpa-repository-map-store-screenshot
懒惰地将 MapStore bean 注入到您初始化地图的 class 也可以:
@Inject
HazelcastConfiguration(@Lazy MyMapStore myMapStore) {
this.myMapStore = myMapStore;
}
在此示例中,MyMapStore
bean 包含一个注入的 MyRepository
bean。
我开始探索 Hazelcast 功能,并且一直在尝试通过 HazelcastRepository 使用 MapStore 作为我的数据库的后写缓冲区。我的目标是在我的 MapStore 中使用 JpaRepository 来加载和存储 to/from 缓存。
我正在使用 Spring Boot,在做了一些研究之后我发现我可以使用 @SpringAware 在 MapStore 中自动连接我的存储库但是每次它到达那里我的 Bean 是null
我得到一个 NullPointerException
。
即使经过许多不同的测试我也无法让它工作 我无法在 MapStore
启用 SpringAware 的配置有问题还是我看错地方了?
找到
我在 Git Repo Here 中提交了我的示例代码。
这里的问题是你正在创建名称为 pr
的 spring bean,当你使用 personRepository
.
在PersonRepository.java
中将pr
更改为personRepository
或者从 PersonRepository.java
("pr")
@Autowired
PersonRepository personRepository;
DAO
@Repository("pr")
public interface PersonRepository extends JpaRepository<Person, Integer> {
@Query("SELECT id FROM Person")
Iterable<Integer> findAllId();
}
调查提供的代码后,我注意到根据我在 Hazelcast 上发现的 GitHub 问题,默认情况下从未启用 @SpringAware。该问题描述了 SpringAware 被禁用是因为它影响了性能,这将我转发到另一个已关闭的票证,解决了使用 SpringManagedContext 通过代码启用注释的问题(避免使用 XML ),但仍不能解决问题。
找到真正的解决方案 here,将 MapLoaderLifecycleSupport 接口添加到您的 MapStore 实现并实现 init 方法,如票证所示:
@Override
public void init(HazelcastInstance hazelcastInstance, Properties properties, String mapName) {
hazelcastInstance.getConfig().getManagedContext().initialize(this);
}
这将强制在 MapStore class 中启用 @SpringAware,因此,能够将任何 spring 组件自动装配到 class 如下图所示。
working-jpa-repository-map-store-screenshot
懒惰地将 MapStore bean 注入到您初始化地图的 class 也可以:
@Inject
HazelcastConfiguration(@Lazy MyMapStore myMapStore) {
this.myMapStore = myMapStore;
}
在此示例中,MyMapStore
bean 包含一个注入的 MyRepository
bean。