Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When using dependency injection, you rely on the container to inject your objects dependencies instead of creating them yourself during execution. For example:</p> <p>Without using dependency injection I would have to write something like this:</p> <pre><code>public class ATMTransferService { private AccountDAO accountDAO; public void transfer(Account from, Account to){ AccountDAO accountDAO = new AccountDAO(); ...Code that transfers money from one account to another and saves it... accountDAO.store(); } </code></pre> <p>Notice that I had to instantiate accountDAO myself, and the responsibility of getting my dependancies was handled by myself.</p> <p>By using spring or some other framework that allows for DI, I would transfer that responsibility tothe container, by writing something like:</p> <pre><code>&lt;bean id="ATMTransfer" class="com.example.ATMTransferService" &gt; &lt;property name="accountDAO" ref="AccDAO" /&gt; &lt;/bean&gt; &lt;bean id="AccDAO" class="com.model.AccountDAO"&gt; &lt;property name="sessionFactory" ref="sessFac" &lt;/bean&gt; &lt;bean id="sessFac" class="..." //Dependencies required for the cration fo session factory. &lt;/bean&gt; </code></pre> <p>Now my class would like like this: public class ATMTransferService { private AccountDAO accountDAO;</p> <pre><code>public void transfer(Account from, Account to){ ...Code that transfers money from one account to another and saves it... accountDAO.store(); } </code></pre> <p>In this case, I've ommited the AccountDAO class, but it would have a dependency on a SessionFactory. You could also use a pre-defined constructor instead of using the default constructor by specifying it on the xml configuration.</p> <p>I hope i haven't simplified it too much, once the benefits of dependency injection are greater than this, you could swap implementation classes by just modifying an xml configuration for starters. You also get a much cleaner code once you remove dependency creation code from your business classes. It is really a great manner to promote interface oriented programming.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload