Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DependencyInjection] Docs for method autowiring#7041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
dunglas wants to merge2 commits intosymfony:masterfromdunglas:pr_20167
Closed
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletionscomponents/dependency_injection/autowiring.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -392,5 +392,91 @@ As for other RAD features such as the FrameworkBundle controller or annotations,
keep in mind to not use autowiring in public bundles nor in large projects with
complex maintenance needs.

Method injection
----------------

By default, the autowiring subsystem only injects dependencies in the constructor of
the service. Constructor injection should always be preferred; however, it is sometimes
convenient to inject dependencies through methods (usually a setter method).

.. versionadded:: 3.2
Method autowiring was added in Symfony 3.2

The ``*`` character can be used to ask the autowiring subsystem to inject dependencies
both in the constructor and in setter methods (starting with ``set``).

.. configuration-block::

.. code-block:: yaml

services:
foo:
class: Foo
autowire: '*'

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="foo" class="Foo" autowire="*" />
</services>
</container>

.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;

// ...
$fooDefinition = new Definition('Foo');
$fooDefinition->setAutowired('*');

$container->setDefinition('foo', $fooDefinition);

Alternatively, the list of methods to use for dependency injection can be configured explicitly:

.. configuration-block::

.. code-block:: yaml

services:
foo:
class: Foo
autowire: ['__construct', 'setFoo', 'notASetter']

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="foo" class="Foo">
<autowire>__construct</autowire>
<autowire>setFoo</autowire>
<autowire>notASetter</autowire>
</service>
</services>
</container>

.. code-block:: php

use Symfony\Component\DependencyInjection\Definition;

// ...
$fooDefinition = new Definition('Foo');
$fooDefinition->setAutowired(array('__construct', 'setFoo', 'notASetter'));

$container->setDefinition('foo', $fooDefinition);


When listing methods to use, dependencies are not automatically injected in the constructor,
it needs to be listed explicitly.

.. _Rapid Application Development: https://en.wikipedia.org/wiki/Rapid_application_development
.. _ROT13: https://en.wikipedia.org/wiki/ROT13

[8]ページ先頭

©2009-2025 Movatter.jp