'@Component @Autowired null

myMessageListener is indeed in the IOC, but why isn't it available. When I use to remove @Component, it is not managed by IOC, and when I create a new object, it will not be prompted to be empty. What's going on? complete error: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.demo.cache.redis.Subscriber]: Constructor threw exception; nested exception is org.springframework.data.redis.RedisSystemException: Unknown redis exception; nested exception is java.lang.IllegalArgumentException: MessageListener must not be null!

@Slf4j
@Component
class MyMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] bytes) {
        log.info("Receive message : " + message);
    }
}
@Slf4j
@Service
public class Subscriber {

    // private  MyMessageListener myMessageListener = new MyMessageListener(); --Success
    /**
     * error: nested exception is java.lang.IllegalArgumentException: MessageListener must not be null!
     */
    @Autowired
    private MyMessageListener myMessageListener;


    public Subscriber(StringRedisTemplate redisTemplate) {
        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        redisConnection.subscribe(myMessageListener, Publish.CHANNEL.getBytes(StandardCharsets.UTF_8));

    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source