原题目:Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程二十(Spring中国教育管理中心)
Apache Geode 的 Spring 数据教程二十
7.8.接线Declarable组件
Apache Geode XML 配置(通常称为cache.xml)容许将用户对象申明为配置的一部分。通常这些对象是CacheLoadersApache Geode 反对的或其余可插入的回调组件。应用原生 Apache Geode 配置,通过 XML 申明的每个用户类型都必须实现Declarable接口,该接口容许通过Properties实例将任意参数传递给申明的类。
在本节中,咱们将形容如何在cache.xml 应用 Spring定义时配置这些可插拔组件,同时放弃在cache.xml. 这容许您的可插拔组件专一于利用程序逻辑,而不是DataSources 其余协作者的地位或创立。
然而,如果您正在启动一个绿地我的项目,建议您间接在 Spring 中配置 Cache、Region 和其余可插入的 Apache Geode 组件。这防止了从Declarable本节中介绍的接口或基类继承。
无关此办法的更多信息,请参阅以下侧边栏。
打消Declarable组件
开发人员能够齐全通过 Spring 配置自定义类型,如配置区域中所述。这样,开发人员就不用实现Declarable接口,还能够从 Spring IoC 容器的所有性能中受害(不仅仅是依赖注入,还有生命周期和实例治理)。
作为Declarable应用 Spring配置组件的示例,请思考以下申明(取自Declarable Javadoc):
<cache-loader>
<class-name>com.company.app.DBLoader</class-name>
<parameter name="URL">
<string>jdbc://12.34.56.78/mydb</string>
</parameter>
</cache-loader>
为了简化解析、转换参数和初始化对象的工作,Apache Geode 的 Spring Data 提供了一个基类 ( WiringDeclarableSupport),它容许通过模板bean 定义连贯 Apache Geode 用户对象,或者,如果短少,执行主动- 通过 Spring IoC 容器接线。要利用此性能,用户对象须要扩大WiringDeclarableSupport,它会主动定位申明BeanFactory 并作为初始化过程的一部分执行连贯。
为什么须要基类?
在以后的 Apache Geode 版本中,没有对象工厂的概念,申明的类型被实例化并按原样应用。换句话说,没有简略的办法来治理 Apache Geode 之外的对象创立。
7.8.1.应用模板bean 定义的配置
应用时,WiringDeclarableSupport尝试首先定位现有的 bean 定义并将其用作接线模板。除非指定,否则组件类名称将用作隐式 bean 定义名称。
让咱们DBLoader看看在这种状况下咱们的申明会是什么样子:
class DBLoader extends WiringDeclarableSupport implements CacheLoader {
private DataSource dataSource;
public void setDataSource(DataSource dataSource){
this.dataSource = dataSource;
}
public Object load(LoaderHelper helper) { ... }
}
<cache-loader>
<class-name>com.company.app.DBLoader</class-name>
<!-- no parameter is passed (use the bean's implicit name, which is the class name) -->
</cache-loader>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="dataSource" ... />
<!-- template bean definition -->
<bean id="com.company.app.DBLoader" abstract="true" p:dataSource-ref="dataSource"/>
</beans>
Apache Geode 的 Spring 数据教程二十
在下面的场景中,因为没有指定参数,一个带有 id/name 的
beancom.company.app.DBLoader被用作连贯由 Apache Geode 创立的实例的模板。对于 bean 名称应用不同约定的状况,能够bean-name在 Apache Geode 配置中传入参数:
<cache-loader>
<class-name>com.company.app.DBLoader</class-name>
<!-- pass the bean definition template name as parameter -->
<parameter name="bean-name">
<string>template-bean</string>
</parameter>
</cache-loader>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="dataSource" ... />
<!-- template bean definition -->
<bean id="template-bean" abstract="true" p:dataSource-ref="dataSource"/>
</beans>
Apache Geode 的 Spring 数据教程二十
该模板bean定义中没有XML申明。容许任何格局(Groovy、正文等)。
7.8.2.应用主动连贯和正文的配置
默认状况下,如果没有找到 bean 定义,WiringDeclarableSupport将 主动拆卸 申明的实例。这意味着除非实例提供任何依赖注入元数据,否则容器将找到对象设置器并尝试主动满足这些依赖关系。然而,开发人员还能够应用 JDK 5 正文为主动拆卸过程提供附加信息。
例如,DBLoader下面的假如申明能够通过DataSource 以下形式注入 Spring-configured :
class DBLoader extends WiringDeclarableSupport implements CacheLoader {
// use annotations to 'mark' the needed dependencies
@javax.inject.Inject
private DataSource dataSource;
public Object load(LoaderHelper helper) { ... }
}
<cache-loader>
<class-name>com.company.app.DBLoader</class-name>
<!-- no need to declare any parameters since the class is auto-wired -->
</cache-loader>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
">
<!-- enable annotation processing --> <context:annotation-config/>
</beans>
Apache Geode 的 Spring 数据教程二十
通过应用 JSR-330 正文,CacheLoader代码失去了简化,因为 的地位和创立DataSource已被内部化,并且用户代码只关怀加载过程。的DataSource可能是事务性的,以提早形式创立,多个对象之间共享或从JNDI检索。这些方面能够通过 Spring 容器轻松配置和更改,而无需接触DBLoader代码。
7.9.反对 Spring Cache 形象
Spring Data for Apache Geode 提供了 Spring Cache Abstraction的实现, 以将 Apache Geode 定位为Spring 缓存基础设施中的缓存提供者。
要应用 Apache Geode 作为反对实现,Spring 的 Cache Abstraction 中的“缓存提供者” ,只需增加到您的配置中:GemfireCacheManager
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:gfe="https://www.springframework.org/schema/geode" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsdhttps://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd
">
<!-- enable declarative caching -->
<cache:annotation-driven/>
<gfe:cache id="gemfire-cache"/>
<!-- declare GemfireCacheManager; must have a bean ID of 'cacheManager' -->
<bean id="cacheManager" class="org.springframework.data.gemfire.cache.GemfireCacheManager"
p:cache-ref="gemfire-cache">
</beans>
在cache-ref该属性CacheManager如果默认缓存bean的名字被应用(即“gemfireCache”),即bean定义是没有必要的<gfe:cache>没有一个明确的标识。
当GemfireCacheManager申明(单例)bean 实例并启用申明性缓存时(在 XML 中<cache:annotation-driven/>或在 JavaConfig 中应用 Spring 的@EnableCaching正文),Spring 缓存正文(例如@Cacheable)标识将应用 Apache Geode Regions 在内存中缓存数据的“缓存” .
这些缓存(即区域)必须在应用它们的缓存注解之前存在,否则会产生谬误。
举例来说,假如您有一个带有CustomerService执行缓存的应用程序组件的客户服务应用程序......
@Service
class CustomerService {
@Cacheable(cacheNames="Accounts", key="#customer.id")
Account createAccount(Customer customer) {
...
}
而后你将须要以下配置。
XML:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:gfe="https://www.springframework.org/schema/geode" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsdhttps://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd
">
<!-- enable declarative caching -->
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.data.gemfire.cache.GemfireCacheManager">
<gfe:cache/>
<gfe:partitioned-region id="accountsRegion" name="Accounts" persistent="true" ...>
...
</gfe:partitioned-region>
</beans>
Java配置:
@Configuration
@EnableCaching
class ApplicationConfiguration {
@Bean
CacheFactoryBean gemfireCache() {
return new CacheFactoryBean();
}
@Bean
GemfireCacheManager cacheManager() {
GemfireCacheManager cacheManager = GemfireCacheManager();cacheManager.setCache(gemfireCache());return cacheManager;
}
@Bean("Accounts")
PartitionedRegionFactoryBean accountsRegion() {
PartitionedRegionFactoryBean accounts = new PartitionedRegionFactoryBean();accounts.setCache(gemfireCache());accounts.setClose(false);accounts.setPersistent(true);return accounts;
}
}
当然,您能够自由选择您喜爱的任何 Region 类型(例如 REPLICATE、PARTITION、LOCAL 等)。
- 应用 Apache Geode 序列化
为了进步 Apache Geode In-memory Data Grid 的整体性能,Apache Geode 反对一种称为 PDX 的专用序列化协定,除了在各种语言平台(Java、Java、 C++ 和 .NET)。
本章探讨 Spring Data for Apache Geode 简化和改良 Apache Geode 在 Java 中的自定义序列化的各种形式。
8.1.连贯反序列化实例
序列化对象具备瞬态数据是相当广泛的。瞬态数据通常取决于它在某个工夫点所处的零碎或环境。例如,aDataSource是特定于环境的。序列化此类信息是无用的,甚至可能是危险的,因为它是特定 VM 或机器的本地信息。对于这种状况,用于 Apache Geode 的 Spring Data 提供了一种非凡的办法Instantiator ,能够在反序列化期间为 Apache Geode 创立的每个新实例执行连贯。
通过这样的机制,你能够依附Spring容器来注入和治理某些依赖,从而能够轻松地从持久数据中拆散transient,并以通明的形式领有丰盛的域对象。
Spring 用户可能会发现这种办法相似于@Configurable)。其WiringInstantiator工作形式与 相似WiringDeclarableSupport,尝试首先将 bean 定义定位为接线模板,否则返回到主动接线。
要应用 SDG Instantiator,请将其申明为 bean,如以下示例所示:
<bean id="instantiator" class="org.springframework.data.gemfire.serialization.WiringInstantiator">
<!-- DataSerializable type -->
<constructor-arg>org.pkg.SomeDataSerializableClass</constructor-arg>
<!-- type id -->
<constructor-arg>95</constructor-arg>
</bean>
在 Spring 容器启动期间,一旦初始化,Instantiator默认状况下,它会向 Apache Geode 序列化零碎注册本人,并SomeDataSerializableClass在反序列化期间对 Apache Geode 创立的所有实例进行连贯。
8.2.主动生成自定义Instantiators
对于数据密集型应用程序,随着数据流入,可能会在每台机器上创立大量实例。Apache Geode 应用反射来创立新类型,然而,对于某些场景,这可能被证实是低廉的。与平常一样,最好进行剖析以量化是否属于这种状况。对于这种状况,Apache Geode 的 Spring Data 容许主动生成Instatiator类,这些类在不应用反射的状况下实例化一个新类型(应用默认构造函数)。以下示例显示了如何创立实例化器:
<bean id="instantiatorFactory" class="org.springframework.data.gemfire.serialization.InstantiatorFactoryBean">
<property name="customTypes">
<map> <entry key="org.pkg.CustomTypeA" value="1025"/> <entry key="org.pkg.CustomTypeB" value="1026"/></map>
</property>
</bean>
后面的定义主动Instantiators为两个类(CustomTypeA和CustomTypeB)生成两个,并在用户 ID1025和下将它们注册到 Apache Geode 1026。两者Instantiators防止应用反射,间接通过Java代码创立实例。