Using setter injection

Using the injection, Spring also provides you with two basic options as the <property> element and p-namespace introduced in Spring 3.0. The p-namespace also reduced verbosity of code in the application, which is the only difference between them, you can choose any one. Let's inject the collaborating beans with the setter injection as follows:

    <bean id="transferService"       
class="com.packt.patterninspring.chapter4.
bankapp.service.TransferServiceImpl"> <property name="accountRepository" ref="accountRepository"/> <property name="transferRepository" ref="transferRepository"/> </bean>
<bean id="accountRepository"
class="com.packt.patterninspring.chapter4.
bankapp.repository.jdbc.JdbcAccountRepository"/>
<bean id="transferRepository"
class="com.packt.patterninspring.chapter4.
bankapp.repository.jdbc.JdbcTransferRepository"/>

In the preceding configuration, the <bean> element of TransferService has two <property> elements which tell it to pass a reference to the beans whose IDs are accountRepository and transferRepository to the setter methods of TransferServiceImpl, as follows:

    package com.packt.patterninspring.chapter4.bankapp.service; 
 
    import com.packt.patterninspring.chapter4.bankapp.model.Account; 
    import com.packt.patterninspring.chapter4.bankapp.model.Amount; 
    import com.packt.patterninspring.chapter4.bankapp.
repository.AccountRepository; import com.packt.patterninspring.chapter4.bankapp.
repository.TransferRepository; public class TransferServiceImpl implements TransferService { AccountRepository accountRepository; TransferRepository transferRepository; public void setAccountRepository(AccountRepository
accountRepository) { this.accountRepository = accountRepository; } public void setTransferRepository(TransferRepository
transferRepository) { this.transferRepository = transferRepository; } @Override public void transferAmmount(Long a, Long b, Amount amount) { Account accountA = accountRepository.findByAccountId(a); Account accountB = accountRepository.findByAccountId(b); transferRepository.transfer(accountA, accountB, amount); } }

In the preceding file, if you use this Spring bean without setter methods, the properties accountRepository and transferRepository will be initialized as null without injecting the dependency.

As of Spring 3.0, the p-namespace, similarly, has a more succinct way of expressing property in XML. For using this namespace, we have to add its schema in the XML file as follows:

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:p="http://www.springframework.org/schema/p" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="transferService"
class="com.packt.patterninspring.chapter4.bankapp.
service.TransferServiceImpl" p:accountRepository-ref="accountRepository" p:transferRepository-
ref="transferRepository"/>
<bean id="accountRepository"
class="com.packt.patterninspring.chapter4.
bankapp.repository.jdbc.JdbcAccountRepository"/>
<bean id="transferRepository"
class="com.packt.patterninspring.chapter4.
bankapp.repository.jdbc.JdbcTransferRepository"/> <!-- more bean definitions go here --> </beans>

Let's now take a look at the dependency injection pattern with Annotation-based configuration.