Dependency injection in Drupal queueworker plugin I am creating a queueworker plugin, and would need the QueryFactory and EntityManagerInterface.
I set these as 'use' statements, and added them in my parameters in __construct(),
but i get following error:


  TypeError: Argument 1 passed to Drupal\custom_example\Plugin\QueueWorker\SalesQueueProcessor::__construct() must be an instance of Drupal\Core\Entity\Query\QueryFactory, array given


My QueueWorker plugin:

namespace Drupal\simplified_bookkeeping\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\custom_example\Entity\BookingEntity;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityManagerInterface;


/**
 * Processes Sales Tasks.
 *
 * @QueueWorker(
 *   id = "sales_queue_processor",
 *   title = @Translation("Generate sales from bank/cash statements in queue"),
 *   cron = {"time" = 10}
 * )
 */
class SalesQueueProcessor extends QueueWorkerBase {

  public function __construct(QueryFactory $entity_query, EntityManagerInterface $entityManager) {
    $this-&gt;entity_query = $entity_query;
    $this-&gt;entityManager = $entityManager;
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($bid) {}
}


What am I doing wrong?
Long story short, your plugin needs to implement ContainerFactoryPluginInterface for services to be injected.

Something like this:

namespace Drupal\simplified_bookkeeping\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\custom_example\Entity\BookingEntity;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Processes Sales Tasks.
 *
 * @QueueWorker(
 *   id = "sales_queue_processor",
 *   title = @Translation("Generate sales from bank/cash statements in queue"),
 *   cron = {"time" = 10}
 * )
 */
class SalesQueueProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryFactory $entity_query, EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this-&gt;entity_query = $entity_query;
    $this-&gt;entityTypeManager = $entityTypeManager;
  }

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container-&gt;get('entity.query'),
      $container-&gt;get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($bid) {}
}


As an aside, the entity manager service is deprecated; the entity type manager should be used instead. You also don't strictly need to inject the entity query service as you can get an instance from the manager, e.g.:

$query = $this-&gt;entityTypeManager-&gt;getStorage('node')-&gt;getQuery();