Java Reference
In-Depth Information
import mjg.spring.entities.Account;
public class AccountProcessor {
private List<Account> accounts;
public void setAccounts(List<Account> accounts) {
this .accounts = accounts;
}
public List<Account> getAccounts() { return accounts; }
public double processAccounts() {
double total = 0.0;
for (Account account : accounts) {
account.withdraw(1.0);
total += 1.0;
}
return total;
}
}
Instead of injecting the AccountDAO into the processor, I gave it a list of accounts as an
attribute. The processAccounts method runs through them, withdrawing a dollar from
each and returning the total. Without the dependency on the AccountDAO , this processor
could be used on any collection of accounts from any source. This has the extra benefit
of always retrieving the complete set of accounts from the DAO. Injecting the account list
would initialize it when the application starts but not update it later.
So how does the collection of accounts get into my processor? The next listing shows the
Java configuration file.
Listing 7.22. A Java configuration file that declares the AccountProcessor bean
package mjg.spring;
import mjg.spring.dao.AccountDAO;
import mjg.spring.services.AccountProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JavaConfig {
@Autowired
private AccountDAO accountDAO;
Search WWH ::




Custom Search