I am studying this book (which I would highly recommend), and I am confused about how the authors explain the way Spring framework can be configured.
You can see some code examples that are used in the book here. (They are available to anyone..) The code I am referring the will be the code from chapter 2, if you would like to take a look.
The book states that there are 3 ways to configure the Spring Container.
XML - based configuration
This will require an xml file that resembles something like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...>
<bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl">
</bean>
</beans>
And then in order to bootstrap Spring, the code that will be used will be:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/com/wiley/beginningspring/ch2/ch2-beans.xml");
I do not have any confusions at this moment.
Java Based Configuration
In this Configuration method, there will be a class for the configuration as follows:
@Configuration
public class Ch2BeanConfiguration {
@Bean
public AccountService accountService() {
AccountServiceImpl bean = new AccountServiceImpl();
bean.setAccountDao(accountDao());
return bean;
}
@Bean
public AccountDao accountDao() {
AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
return bean;
}
}
and the code that is responsible for bootstrapping Spring looks like:
ApplicationContext applicationContext
= new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class);
So up to here, all is clear for me. (Kind of..) I would also like to note that, here we actually have an Annotation which is called @Configuration...
Annotation Based Configuration
The last configuration method available, explained in the book is the Annotation Based Configuration.
There is an xml file just like we had in the XML-Based Configuration, however a much smaller one:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...>
<context:component-scan base-package="com.wiley.beginningspring.ch2"/>
</beans>
All the beans have Annotations such as:
@Component, @Service
etc..
And all the dependencies have the annotations:
@Autowired
so that beans can be injected.
The way Spring framework bootstrapped in this configuration method is as follows:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/ch2-beans.xml");
Here are my questions:
Why is the (so-called) Annotation Based Configuration actually using ClassPathXmlApplicationContext but not AnnotationConfigApplicationContext above? The latter seems much more appropriate to be used in a Configuration that has the words "Annotation Based" in it, isn 't it?
The Java Based Configuration explained in the book seems like what should be called Annotation Based Configuration.?
And the way Annotation Based Configuration explained in the book actually seems to me something like: XML-Based Configuration with Autowired beans. It does not even have the @Configuration annotation, which the "Java Based Configuration" has..
How many ways are there to configure Spring framework?