Java Reference
In-Depth Information
@Bean
public AccountProcessor accountProcessor() {
AccountProcessor ap = new AccountProcessor();
ap.setAccounts(accountDAO.findAllAccounts());
return ap;
}
}
The @Configuration annotation indicates that this is a Java configuration file that
defines beans for Spring. Each bean is defined with the @Bean annotation. The name of
the method is the name of the bean, and the return type is the class for the bean. Inside the
method my job is to instantiate the bean, configure it appropriately, and return it.
The implementation of a bean method can be as simple as instantiating the bean and return-
ing it, setting whatever properties are needed along the way. In this case, though, I decided
to autowire in the AccountDAO bean (which was picked up in the component scan) and
then use the DAO to retrieve all the accounts and put them in the processor.
The next listing shows a Spock test to prove that the system is working. It relies on the em-
bedded database again, which, as you may recall, configures three accounts.
Listing 7.23. A Spock test to check the behavior of the AccountProcessor
package mjg.spring.services
import mjg.spring.dao.AccountDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration
import org.springframework.transaction.annotation.Transactional
import spock.lang.Specification
@ContextConfiguration("classpath:applicationContext.xml")
@Transactional
class AccountProcessorSpec extends Specification {
@Autowired
AccountProcessor accountProcessor
@Autowired
AccountDAO dao
def "processing test accounts should yield 3"() {
given: def accounts = dao.findAllAccounts()
Search WWH ::




Custom Search