How to inject configuration values into services? In Symfony 2 when defining a service it is possible to inject configuration parameters by referencing them with %parameter.name% strings. For example:

services:
  app.mailer:
    class:        AppBundle\Mailer
    arguments:    ['%app.mailer.transport%']


But what is the correct Drupal 8 approach to injecting configuration values into services? Of course I don't want to use \Drupal::config() inside a service class. Passing configuration values each time a service is referenced does not make much sense either.

I know that I can inject the config service itself and then get configuration values from it, but that looks a bit crappy as my service itself has know how to read data from configuration service. For example:

# Yaml service configuration
services:
  app.mailer:
    class:        mail_module\Mailer
    arguments:    ['@config.factory']


PHP

&lt;?php
class Mailer {
  public function __construct($config) {
    $this-&gt;mailTransport = $config-&gt;get('mail.config')-&gt;get('transport');
  }
}


Is there any other way to do this?
You can use a factory for your app.mailer service. The factory take cares of retrieving the configuration for the service. The service can stay decoupled from the configuration service and does not need to know how the config parameters are named.

services:
  app.mailer:
    class:       Drupal/mail_module/Mailer
    factory:      Drupal/mail_module/MailerFactory:create
    arguments:    ['@config.factory']


class MailerFactory {
  static function create($config) {
    return new Mailer($config-&gt;get('mail.config')-&gt;get('transport'));
  }
}

class Mailer {
  public function __construct($transport) {
    $this-&gt;mailTransport = $transport;
  }
}