Home > OS >  Where was this object injected?
Where was this object injected?

Time:01-20

I'm learning Spring. In the following code, kafkaTemplate instance was not injected with @AutoWired in line 7. how can it be used in TransactionsListener method:

@Service
public class TransactionsListener {

    private static final Logger LOG = LoggerFactory
        .getLogger(TransactionsListener.class);

    KafkaTemplate<Long, Order> kafkaTemplate;               // line 7
    AccountRepository repository;

    public TransactionsListener(KafkaTemplate<Long, Order> kafkaTemplate, AccountRepository repository) {
        this.kafkaTemplate = kafkaTemplate;
        this.repository = repository;
    }

    @KafkaListener(
            id = "transactions",
            topics = "transactions",
            groupId = "a",          
            concurrency = "3")                  

    @Transactional("kafkaTransactionManager")   
    public void listen(Order order) {
        LOG.info("Received: {}", order);
        process(order);
    }

    private void process(Order order) {/*...*/}

CodePudding user response:

Spring will automatically inject beans that are constructor parameters. @Autowired is not required.

Such beans must exist and be unique, or exactly one must be marked @Primary, or one of the dependency bean's name must match the parameter name.

  • Related