Assembling via Spring's Java Config
Learn how to assemble your web application with the help of Spring's Java configuration.
We'll cover the following...
Java Config
While classpath scanning is the cudgel of application assembly, Spring’s Java Config is the scalpel. This approach is similar to the plain code approach introduced earlier in this chapter, but it’s less messy and provides us with a framework so that we don’t have to code everything by hand.
In this approach, we create configuration classes, each responsible for constructing a set of beans that are to be added to the application context.
For example, we could create a configuration class that is responsible for instantiating all our persistence adapters:
@Configuration@EnableJpaRepositoriesclass PersistenceAdapterConfiguration {@BeanAccountPersistenceAdapter accountPersistenceAdapter(AccountRepository accountRepository,ActivityRepository activityRepository,AccountMapper accountMapper){return new AccountPersistenceAdapter(accountRepository,activityRepository,accountMapper);}@BeanAccountMapper accountMapper(){return new AccountMapper();}}
The @Configuration
annotation
The @Configuration
annotation marks this class as a configuration class to be picked
up by Spring’s classpath scanning. So, in this case, we’re still using classpath ...