Reputation: 53
I started exploring about Hazelcast functionalities and I've been trying use a MapStore as a Write-Behind buffer for my database through HazelcastRepository. My goal is to use a JpaRepository inside my MapStore to load and store to/from the cache.
I'm using Spring Boot and after doing some research i found that i could use the @SpringAware to Autowire my Repository inside MapStore but every-time it gets there my Bean is null
and I get a NullPointerException
.
I can't get it working even after lots of different tests I was not able to Autowire my bean inside the MapStore
is something wrong with this configuration to enable SpringAware or am I looking at the wrong place?
Found This stackoverflow post and it gave me clues but I still couldn't figure out the issue since most configurations were xml and not java. Also Found this Github Issue on how to configure SpringAware in Hazelcast through Java configurations
And I committed my example code in this Git Repo Here.
Upvotes: 5
Views: 3461
Reputation: 3023
Lazily injecting the MapStore bean to the class where you initialise your maps works too:
@Inject
HazelcastConfiguration(@Lazy MyMapStore myMapStore) {
this.myMapStore = myMapStore;
}
In this example the MyMapStore
bean contains an injected MyRepository
bean.
Upvotes: 0
Reputation: 86
After investigation the provided code, I noticed that the @SpringAware was never enabled by default as per a GitHub issue I found on Hazelcast. The issue described that SpringAware was disabled because it was affecting the performance, which forwarded me to another closed ticket solving enabling the annotation by code using the SpringManagedContext (avoiding the use of XML), but it doesn't still solve the issue.
The real solution was found here, add MapLoaderLifecycleSupport interface to your MapStore implementation and implement the init method as shown in the ticket:
@Override
public void init(HazelcastInstance hazelcastInstance, Properties properties, String mapName) {
hazelcastInstance.getConfig().getManagedContext().initialize(this);
}
This will force @SpringAware to be enabled in the MapStore class, therefore, being able to autowire any spring component into the class as shown in the image below.
working-jpa-repository-map-store-screenshot
Upvotes: 7
Reputation: 5871
Here the problem is you are creating the spring bean with name pr
, when auto wiring you are using personRepository
.
Either change pr
to personRepository
in PersonRepository.java
OR remove ("pr")
from PersonRepository.java
@Autowired
PersonRepository personRepository;
DAO
@Repository("pr")
public interface PersonRepository extends JpaRepository<Person, Integer> {
@Query("SELECT id FROM Person")
Iterable<Integer> findAllId();
}
Upvotes: 1