在重写方法中,Spring @Autowired字段为null

时间:2016-05-27 09:30:16

标签: java spring autowired

我有一个实现MessageListener的类,用于Spring Data Redis。它适用于Redis,但我有一个奇怪的行为。我有一个test()方法来测试messageService实例是否为null。它需要调用Jpa存储库以获取MySql DB中的持久性。奇怪的行为是,如果在getNewMessage()和printWelcome()方法中调用它(然后我可以使用CRUD方法进行持久化)它不会为null,但是在onMessage中它是 null ()方法被org.springframework.data.redis.connection.MessageListener类覆盖。这是我的Controller类:

@Controller
public class StatoController implements MessageListener{


  @Autowired
  private MessageService messageService;

  public static final List<DeferredResult<Message>> messageDeferredResultList = new ArrayList<>();

  @RequestMapping(value = "sec/redis", method = RequestMethod.GET, produces = "application/json")
  @ResponseBody
  public DeferredResult<Message> getNewMessage() throws Exception {
    Long deferredResultTimeout = 0L;

    final DeferredResult<Message> deferredResult = new DeferredResult<>(deferredResultTimeout);

    deferredResult.onCompletion(() -> {
        messageDeferredResultList.remove(deferredResult);
    });

    deferredResult.onTimeout(() -> {
        messageDeferredResultList.remove(deferredResult);
    });

    messageDeferredResultList.add(deferredResult);

    try{
        //not null
        List<Message> list=messageService.findAll();
    }
    catch(Exception ex){ex.printStackTrace();}

    return deferredResult;
  }

  @RequestMapping(value = {"sec/stato.html"}, method = RequestMethod.GET)
  public ModelAndView printWelcome(ModelMap model) {

    try{
        //not null
        List<Message> list=messageService.findAll();
    }
    catch(Exception ex){ex.printStackTrace();}

    ModelAndView mav = new ModelAndView("secured/stato");
    return mav;
  }


  @Override
  public void onMessage(Message msg, byte[] bytes) {

    try{
        //NullPointerException
        List<Message> list=messageService.findAll();
    }
    catch(Exception ex){ex.printStackTrace();}
    // set the deferred results for the user
    messageDeferredResultList.stream().forEach((deferredResult) -> {
        deferredResult.setResult(msg);
    });

  }

当消息到达并且调用onMessage方法时,这是堆栈跟踪:

  

显示java.lang.NullPointerException       在infn.lns.acceleratori.controller.StatoController.onMessage(StatoController.java:95)       在org.springframework.data.redis.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:299)       在org.springframework.data.redis.listener.RedisMessageListenerContainer.executeListener(RedisMessageListenerContainer.java:245)       在org.springframework.data.redis.listener.RedisMessageListenerContainer.processMessage(RedisMessageListenerContainer.java:235)       在org.springframework.data.redis.listener.RedisMessageListenerContainer $ 1.run(RedisMessageListenerContainer.java:960)       在java.lang.Thread.run(Thread.java:745)

当Redis发出消息时,会调用onMessage()方法。我在context-config.xml中正确添加了这个:

<context:annotation-config/>
<context:component-scan base-package="org.springframework.data.redis, infn.lns.acceleratori"/>

这是包含数据redis的bean声明的配置类:

@Configuration
public class AppConfig {

  @Bean
  JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
  }

  @Bean
  RedisTemplate< String, Object > redisTemplate() {
    final RedisTemplate< String, Object > template =  new RedisTemplate<  >();
    template.setConnectionFactory( jedisConnectionFactory() );
    template.setKeySerializer( new StringRedisSerializer() );
    template.setHashValueSerializer( new GenericToStringSerializer<  >( Object.class ) );
    template.setValueSerializer( new GenericToStringSerializer<  >( Object.class ) );
    return template;
  }

  @Bean
  MessageListenerAdapter messageListener() {
    return new MessageListenerAdapter( new RedisMessageListener() );
  }

  @Bean
  RedisMessageListenerContainer redisContainer() {
    final RedisMessageListenerContainer container = new RedisMessageListenerContainer();

    container.setConnectionFactory( jedisConnectionFactory() );
    container.addMessageListener( messageListener(), new ChannelTopic( "queue" ) );

    return container;
  }

}

这是作为Bean创建的MessageServiceImpl:

@Service
public class MessageServiceImpl implements MessageService {

  @Resource
  private MessageRepository messageRepository;

  @Override
  @Transactional
  public Message create(Message event) {
    Message createdMessage = event;
    return messageRepository.save(createdMessage);
  }

我想知道为什么messageRepository的实例在getNewMessage()和printWelcome()方法中正确自动装配,但在覆盖的onMessage()方法中没有。

修改

现在我将我的监听器和控制器分成两个不同的类。我有这个新课程:

public class RedisMessageListener implements MessageListener {

  @Autowired
  private MessageService messageService;

  @Override
  public void onMessage(Message message, byte[] paramArrayOfByte) {
    System.out.println(messageService==null);

    messageDeferredResultList.stream().forEach((deferredResult) -> {
        deferredResult.setResult(message);
    });
  }
}

但是messageService始终为null。它只是一个豆!

2 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案,感谢@dunni。我自己还在创建RedisMessageListener的实例。允许Spring创建它(通过添加@Component注释)并将其注入我创建MessageListenerAdapter的方法中,使用@Autowired,我解决了这个问题。

答案 1 :(得分:0)

@Vipul

这意味着你的 MessageListenerAdapter 配置自己创建了一个 TaskEventSubscriber bean,而不是 spring。

错误:

    @Bean
    MessageListenerAdapter taskEventListener() {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(new TaskEventSubscriber());
        return messageListenerAdapter;
    }

正确:


@Autowired
private TaskEventSubscriber taskEventSubscriber;


@Bean
    MessageListenerAdapter taskEventListener() {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(taskEventSubscriber);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        messageListenerAdapter.setSerializer(stringSerializer);
        return messageListenerAdapter;
    }

(抱歉我不能评论)