将类注入InfiniSpan侦听器

时间:2019-05-07 10:14:27

标签: java infinispan

我试图将一个类注入infinispan侦听器中;不幸的是,注入操作不起作用,并且myObject始终为null。我尝试注入属于MyClass类的对象;     但是,方法a.doSomething()启动总是抛出NullPointerException,因为a注入不正确。有没有办法用CDI注射来解决这个问题?

    enter code here



package org.infinispan.quickstart.jbossas7;

import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryLoaded;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
import org.infinispan.notifications.cachelistener.event.CacheEntryActivatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryLoadedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An Infinispan listener that simply logs cache entries being created and removed
 * 
 * @author Pete Muir
 * 
 */
@Listener
public class LoggingListener {

    private static final String myClassName = LoggingListener.class.getName();
    private static final Logger logger = LoggerFactory.getLogger(myClassName);

    @Inject
    MyClass a;

    private Log log = LogFactory.getLog(LoggingListener.class);

    @CacheEntryCreated
    public void observeAdd(CacheEntryCreatedEvent<?, ?> event) {
        log.infof("Cache entry with key {} added in cache {}", event.getKey(), event.getCache());
        logger.info("Cache entry with key {} added in cache {}", event.getKey(), event.getCache());
        a.domSomething();
    }

    @CacheEntryRemoved
    public void observeRemove(CacheEntryRemovedEvent<?, ?> event) {
        log.infof("Cache entry with key {} removed in cache {}", event.getKey(), event.getCache());
        logger.info("Cache entry with key {} removed in cache {}", event.getKey(), event.getCache());
        a.domSomething();
    }

    @CacheEntryLoaded
    public void observeLoad(CacheEntryLoadedEvent<?, ?> event) {
        log.infof("Cache entry with key {} added in cache {}", event.getKey(), event.getCache());
        logger.info("Cache entry with key {} loaded in cache {}", event.getKey(), event.getCache());
        a.domSomething();
    }

    @CacheEntryActivated
    public void observeActivate(CacheEntryActivatedEvent<?, ?> event) {
        log.infof("Cache entry with key {} added in cache {}", event.getKey(), event.getCache());
        logger.info("Cache entry with key {} activated in cache {}", event.getKey(), event.getCache());
        a.domSomething();
    }

}

0 个答案:

没有答案