此内容没有您所选择的语言版本。
15.2. Using the Infinispan CDI Module
The Infinispan CDI module can be used for the following purposes:
- To configure and inject Infinispan caches into CDI Beans and Java EE components.
- To configure cache managers.
- To control storage and retrieval using CDI annotations.
15.2.1. Configure and Inject Infinispan Caches 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
15.2.1.1. Inject an Infinispan Cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
An Infinispan cache is one of the multiple components that can be injected into the project's CDI beans.
The following code snippet illustrates how to inject a cache instance into the CDI bean:
public class MyCDIBean {
@Inject
Cache<String, String> cache;
}
public class MyCDIBean {
@Inject
Cache<String, String> cache;
}
15.2.1.2. Inject a Remote Infinispan Cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The code snippet to inject a normal cache is slightly modified to inject a remote Infinispan cache, as follows:
public class MyCDIBean {
@Inject
RemoteCache<String, String> remoteCache;
}
public class MyCDIBean {
@Inject
RemoteCache<String, String> remoteCache;
}
15.2.1.3. Set the Injection's Target Cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The following are the three steps to set an injection's target cache:
- Create a qualifier annotation.
- Add a producer class.
- Inject the desired class.
15.2.1.3.1. Create a Qualifier Annotation 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To use CDI to return a specific cache, create custom cache qualifier annotations as follows:
Use the created
@SmallCache qualifier to specify how to create specific caches.
15.2.1.3.2. Add a Producer Class 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The following code snippet illustrates how the
@SmallCache qualifier (created in the previous step) specifies a way to create a cache:
The elements in the code snippet are:
@ConfigureCachespecifies the name of the cache.@SmallCacheis the cache qualifier.
15.2.1.3.3. Inject the Desired Class 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Use the
@SmallCache qualifier and the new producer class to inject a specific cache into the CDI bean as follows:
public class MyCDIBean {
@Inject @SmallCache
Cache<String, String> mySmallCache;
}
public class MyCDIBean {
@Inject @SmallCache
Cache<String, String> mySmallCache;
}