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?
There are 3 different ways to configure a class as Spring Bean. XML Configuration is the most popular configuration. The bean element tag is used in xml context file to configure a Spring Bean. Using Java Based Configuration, you can configure a Spring bean using @Bean annotation.
The Spring framework consists of seven modules which are shown in the above Figure. These modules are Spring Core, Spring AOP, Spring Web MVC, Spring DAO, Spring ORM, Spring context, and Spring Web flow.
Spring is a framework which helps to connect different components together. There are many modules for IOC, AOP, Web MVC etc. Spring Framework is an open source application framework and inversion of control container for the Java platform.
To avoid confusion, we should understand, that configuration definition and bean definition are two different things. There are three ways to define configuration, available in Spring 4 by default:
And there are two ways to add bean definitions into application:
configuration inside bean definition, when you add beans manually by declaration right in configuration.
In this case definition will be based on configuration type. For xml-config it will be <bean/>
tag, for java-based config - method with @Bean
annotation and beans {...}
construction for Groovy.
annotation based bean definition, when you mark bean classes with specific annotations (like @Component
, @Service
, @Controller
etc). This type of config uses classpath scanning.
In this case you have to specify directive for scanning classpath. For xml-config it will be <context:component-scan base-package="..."/>
, for java-config - @ComponentScan
annotation, for Groovy ctx.'component-scan'(...)
invocation.
As you see, you can use configurations and bean definitions in different combinations.
Note, that if you use xml based config, you can choose approach to drive dependency injection: manually in xml, or by using annotations (@Autowire
, @Required
etc). In late case you have to define <context:annotation-config/>
. But do not confuse bean definition and dependency injection control.
Now based on this point of view lets try to answer your questions:
Why is the (so-called) Annotation Based Configuration actually using ClassPathXmlApplicationContext but not AnnotationConfigApplicationContext above?
Book's author mixed up concepts. Actually, this is a xml-based configuration with annotation-based bean definition.
The Java Based Configuration explained in the book seems like what should be called Annotation Based Configuration.?
You're right - Java based configuration really actively uses annotations, and could be called Annotation based. But annotation is a part of Java. In addition this is a traditional term, specified in documentation.
How many ways are there to configure Spring framework?
So, by default, we have three ways to describe configuration, and two ways to define beans. That turns six ways to configure Spring framework(by default). But, of course, all of this ways can be used in combination with each other.
The easiest way to understand this is to look into the long history of the framework how this was developed.
XML based configuration - this was there from the the beginning - version 1 - see javadoc for ClassPathXmlApplicationContext. This was around march 2004, the time of J2EE 1.4, which had HUGE xml configuration and Spring was big simplification (XML as well, but simpler). This uses XML for everything, including specifying autowiring, or what dependencies go where directly (your ref="accoundDao" example).
Annotation based configuration - in Spring 2.5 - this came as a reaction to Java EE 5, new anotations like @Autowired were introduced, there was still some context configuration in XML files - usually you would define which packages were to be scanned and rest of it was done automatically based on annotations - hence the name.
Java based configuration came with Spring 3, was improved in later versions. This is when AnnotationConfigApplicationContext and Configuration annotation were introduced - you could potentially drop XML entirely -> java based config. Although this became practical only later with version 4+, because of large number of xml helper tags for aop, jdbc etc.
Beside these 3 (2 actually as 1 and 2 use the same ApplicationContext class), are other ways to create a context:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With