2017-12-13 4 views
0

私は、永続化レイヤとしてcassandra 2.1.9を使用してApache Ignite 2.3を使用しています。 私は、dbからデータを保存して取得するcacheStoreFactoryクラスを使用しています。 このクラスのいくつかの依存関係を自動配線していますが、nullになっています。ここスプリングブートautowiredオブジェクトがignite persistence cachestoreファクトリクラスでヌルに設定されています

私のサンプルのIgnite confifurationファイルされる:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     "> 
    <description>Main Spring file for ignite configuration.</description> 

    <bean id="cacheIgniteBean" class="org.apache.ignite.IgniteSpringBean"> 
     <property name="configuration"> 
      <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> 
       <property name="dataStorageConfiguration"> 
        <bean class="org.apache.ignite.configuration.DataStorageConfiguration"> 
         <property name="dataRegionConfigurations"> 
          <list> 
           <!-- 
            Defining a data region that will consume up to 2 GB of RAM. 
           --> 
           <bean class="org.apache.ignite.configuration.DataRegionConfiguration"> 
            <!-- Custom region name. --> 
            <property name="name" value="2GB_Region"/> 

            <!-- 500 MB initial size (RAM). --> 
            <property name="initialSize" value="#{500L * 1024 * 1024}"/> 

            <!-- 2 GB maximum size (RAM). --> 
            <property name="maxSize" value="#{2L * 1024 * 1024 * 1024}"/> 

            <!-- Enabling RANDOM_LRU eviction for this region. --> 
            <property name="pageEvictionMode" value="RANDOM_LRU"/> 
           </bean> 
          </list> 
         </property> 
        </bean> 
       </property> 
       <property name="cacheConfiguration"> 
        <list> 
         <bean class="org.apache.ignite.configuration.CacheConfiguration"> 

          <property name="name" value="item"/> 
          <property name="cacheMode" value="PARTITIONED"/> 
          <property name="atomicityMode" value="ATOMIC"/> 
          <property name="backups" value="0"/> 
          <property name="cacheStoreFactory"> 
           <bean class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf"> 
            <constructor-arg value="com.tgt.gom.cacheserver.store.ItemCacheStore"/> 
           </bean> 

          </property> 

          <property name="readThrough" value="${ignite.config.cache.item.readThrough}"/> 
          <property name="writeThrough" value="${ignite.config.cache.item.writeThrough}"/> 
          <property name="writeBehindEnabled" value="${ignite.config.cache.item.writeBehindEnabled}"/> 
          <property name="writeBehindFlushSize" 
             value="${ignite.config.cache.item.writeBehindFlushSize}"/> 
          <property name="writeBehindFlushFrequency" 
             value="${ignite.config.cache.item.writeBehindFlushFrequency}"/> 
          <property name="writeBehindFlushThreadCount" 
             value="${ignite.config.cache.item.writeBehindFlushThreadCount}"/> 
          <property name="writeBehindBatchSize" 
             value="${ignite.config.cache.item.writeBehindBatchSize}"/> 
         </bean> 

         <bean class="org.apache.ignite.configuration.CacheConfiguration"> 

          <property name="name" value="location"/> 
          <property name="cacheMode" value="PARTITIONED"/> 
          <property name="atomicityMode" value="ATOMIC"/> 
          <property name="backups" value="0"/> 
          <property name="cacheStoreFactory"> 
           <bean class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf"> 
            <constructor-arg value="com.tgt.gom.cacheserver.store.LocationCacheStore"/> 
           </bean> 
          </property> 
          <property name="readThrough" value="${ignite.config.cache.item.readThrough}"/> 
          <property name="writeThrough" value="${ignite.config.cache.item.writeThrough}"/> 
          <property name="writeBehindEnabled" value="${ignite.config.cache.item.writeBehindEnabled}"/> 
          <property name="writeBehindFlushSize" 
             value="${ignite.config.cache.item.writeBehindFlushSize}"/> 
          <property name="writeBehindFlushFrequency" 
             value="${ignite.config.cache.item.writeBehindFlushFrequency}"/> 
          <property name="writeBehindFlushThreadCount" 
             value="${ignite.config.cache.item.writeBehindFlushThreadCount}"/> 
          <property name="writeBehindBatchSize" 
             value="${ignite.config.cache.item.writeBehindBatchSize}"/> 
         </bean> 

        </list> 
       </property> 

       <!--<property name="includeEventTypes"> 
        <util:constant static-field="org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION"/> 
       </property>--> 
       <property name="failureDetectionTimeout" value="5000"/> 
       <property name="discoverySpi"> 
        <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> 
         <property name="ipFinder"> 
          <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder"> 
           <!-- <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder"> --> 
           <property name="addresses"> 
            <list> 
             <value>127.0.0.1:47500..47509</value> 
            </list> 
           </property> 
          </bean> 
         </property> 
        </bean> 
       </property> 
      </bean> 
     </property> 
    </bean> 
</beans> 

ここでは私のItemCacheStoreクラスのコードです:

@Slf4j 
@Service 
public class ItemCacheStore extends CacheStoreAdapter<String, ItemV1DTO> implements Serializable { 
    private static final long serialVersionUID = 1L; 


    @Autowired 
    private ItemRepository itemRepository; 

    @Autowired 
    private ItemCacheStoreAsync itemCacheStoreAsync; 

    private static final String LOG_OP_INFO = "Item_Cache_Store"; 


    @Override 
    public ItemV1DTO load(String item_id) throws CacheLoaderException { 
     ItemV1DTO itemV1DTO = null; 
     System.out.println("in item cache store "); 

     try { 
      ItemEntity itemEntity = itemRepository.findOne(item_id); 
      if (itemEntity != null) { 
       itemV1DTO = mapToItemDTO(itemEntity); 
      } 
     } catch (Exception e) { 
      throw new CacheLoaderException("failed to load item data from cassandra" + e.getMessage()); 
     } 
     return itemV1DTO; 
    } 
} 

ItemCacheStoreクラスでロードメソッドが呼び出されたときに、itemRepositoryフィールドはnullです。しかし、別のコントローラクラスで同じItemRepositoryのBeanをオートワイヤすると、正常に動作します。私が気づい

もう一つは、私がItemCacheStoreクラスで@PostConstructアノテーションで一つの方法を置くならば、その時点で私はItemRepositoryの依存性が注入されてしまったが、loadメソッドが呼び出されたときに、それが再びnullで見ることができるということです。

<property name="cacheStoreFactory"> 
    <bean class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf"> 
     <constructor-arg value="com.tgt.gom.cacheserver.store.ItemCacheStore"/> 
    </bean> 
</property> 

あなたがタイプFactoryBuilderののSpring Beanを作成していると、あなたはクラス名ItemCacheStoreを渡す:

答えて

0

問題は、次のような構成です。画面の後ろでは、FactoryBuilder新しいインスタンスItemCacheStoreを作成します。この新しいインスタンスはSpringによって管理されないため、すべてのフィールドはnullになります。

  • 一つ@Service注釈にSpringコンテナのおかげで作成された:

    だから、基本的に、あなたはItemCacheStoreの2つのインスタンスになってしまいます。すべての自動フィールドが機能します。

  • もう1つはFactoryBuilderによって作成されます。それはSpringによって管理されず、すべての自動フィールドはnullになります。この問題を解決するには

、いくつかの可能性があります:

  1. 異なる工場を使用するかではなく、新しいものを作成するよりも春のマネージドBeanを使用することを独自のものを書くには。ドキュメントによると、既にCassandraCacheStoreFactoryがあります。
  2. this answerで説明したようにAutowireHelperを使用するか、スプリングSpringBeanAutowiringSupportを使用して、Spring以外の管理対象BeanにBeanを挿入します。

Why is my Spring @Autowired field null?

関連する問題