Как внедрить компонент зависимости в реализацию GridCacheStore?

Моя конфигурация:

<bean parent="cache-template">
    <property name="name" value="yagoLabel" />
    <property name="cacheMode" value="PARTITIONED" />
    <property name="atomicityMode" value="TRANSACTIONAL" />
    <property name="distributionMode" value="PARTITIONED_ONLY" />
    <property name="backups" value="1" />
    <property name="store">
        <bean class="id.ac.itb.ee.lskk.lumen.yago.YagoLabelCacheStore" autowire="byType" init-method="init" />
    </property>
    <property name="writeBehindEnabled" value="true" />
    <property name="writeBehindFlushSize" value="102380" />
    <property name="writeBehindFlushFrequency" value="30000" />
    <property name="writeBehindBatchSize" value="10240" />
    <property name="swapEnabled" value="false" />
    <property name="evictionPolicy">
        <bean class="org.gridgain.grid.cache.eviction.lru.GridCacheLruEvictionPolicy">
            <property name="maxSize" value="102400" />
        </bean>
    </property>
</bean>

И я запускаю GridGain следующим образом:

Моя реализация GridCacheStore:

public class YagoLabelCacheStore extends GridCacheStoreAdapter<String, YagoLabel> {

    private static final Logger log = LoggerFactory
        .getLogger(YagoLabelCacheStore.class);
    private DBCollection labelColl;

   @GridSpringResource(resourceName="mongoDb")
   private DB db;
   @Inject
   private GridGainSpring grid;

   @PostConstruct
   public void init() {
   log.info("Grid is {}", grid);
   labelColl = db.getCollection("label");
}

Я запускаю GridGain следующим образом:

String entityId = "Muhammad";

try (AnnotationConfigApplicationContext appCtx 
          = new AnnotationConfigApplicationContext(LumenConfig.class)) {
    Grid grid = appCtx.getBean(Grid.class);
    GridCache<String, YagoLabel> labelCache = YagoLabel.cache(grid);
    log.info("Label for {}: {}", entityId, labelCache.get(entityId));
}

LumenConfig Конфигурация Spring содержит DB bean-компонент с именем mongoDb.

Однако это выдает NullPointerException, потому что db не вводится должным образом. Я пробовал @Inject GridGainSpring просто для пробы, и даже сам GridGainSpring не вводится.

Я также попытался установить <property name="db" ref="mongoDb"/> в XML-файле конфигурации GridGain, но Spring жалуется, что не может найти компонент.

Мой обходной путь — поместить его в поле public static, но это слишком хакерски: itb/ee/lskk/lumen/yago/YagoLabelCacheStore.java" rel="nofollow">https://github.com/ceefour/lumen-kb/blob/b8445fbebd227fb7ac337c758a60badb7ecd3095/cli/src/main/java/id/ac/ itb/ee/lskk/lumen/yago/YagoLabelCacheStore.java


person Hendy Irawan    schedule 07.07.2014    source источник


Ответы (1)


Путь к загрузите GridConfiguration с помощью Spring, затем передайте его GridGainSpring.start() :

// "classpath:" is required, otherwise it won't be found in a WAR
@ImportResource("classpath:id/ac/itb/ee/lskk/lumen/core/lumen.gridgain.xml")
@Configuration
public static class GridGainConfig {

    @Inject
    private ApplicationContext appCtx;
    @Inject
    private GridConfiguration gridCfg;

    @Bean(destroyMethod="close")
    public Grid grid() throws GridException {
        return GridGainSpring.start(gridCfg, appCtx);
    }

}

:-)

person Hendy Irawan    schedule 07.07.2014