How many ways are there to configure the Spring framework? What are the differences between them technically? (Not pros or cons..) How many ways are there to configure the Spring framework? What are the differences between them technically? (Not pros or cons..) spring spring

How many ways are there to configure the Spring framework? What are the differences between them technically? (Not pros or cons..)


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:

  • xml-based configuration, when you describe configuration in xml file;
  • java-based configuration, when configuration is Java class, marked with specific annotations;
  • groovy-based configuration, when configuration is file with Groovy code;

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 usingClassPathXmlApplicationContext but notAnnotationConfigApplicationContext 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 whatshould 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:


At first, I want to give thanks Ken Bekov for his more resourceful answer. I have tried to improvise his answer so that anyone can learn more on this area.

Configuration Definition:

Spring 4 contains 3 ways to define its configuration. They are

enter image description here

Advantages of the annotation:

  1. All the information is in a single file (no need to open two files to configure a given behavior)

  2. When the class changes, no need to modify the xml file

  3. Annoations often said to be more intuitive and robust when re-factoring application code. Also they benefit from a better IDE guidance like guise provides. But they mix application code with DI concerns. An application gets dependent on a framework. Clear separation is almost impossible. Annotations are also limited when describing different injection behaviour at the same place (constructor, field) dependent on other circumstances (e.g. robot legs problem). Moreover they don't allow to treat external classes (library code) like your own source. Therefore they are considered to run faster than XML.

Advantages of xml file:

  1. Clear separation between the POJO and its behavior

  2. When you do not know which POJO is responsible for the behavior, it is easier to find that POJO (searching in a subset of files rather than all the source code)

  3. XML has the only benifit of a declarative style that is defined clearly separated from the application code itself. That stays independent from DI concerns. The downsides are verbosity, poor re-factoring robustness and a general runtime failure behaviour. There is just a general (XML) tool support with little benefit compared to IDE support for e.g. Java. Besides this XML comes with a performance overhead so it is usually slower than code solutions.

XML and Annotation Based Link:

  1. http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-annotation-config
  2. Annotations vs XML, advantages and disadvantages
  3. Java Dependency injection: XML or annotations
  4. Spring annotation-based DI vs xml configuration?
  5. Xml configuration versus Annotation based configuration

Groovy Based Link:

  1. https://objectpartners.com/2016/01/12/using-groovy-based-spring-configuration/
  2. http://blog.andresteingress.com/2014/02/14/grails-java-based-spring-config/

Bean Definition:

There are 2 ways to bean Definition:enter image description here

Scanning classpath:

For xml-config it will be <context:component-scan base-package="..."/>, for java-config - @ComponentScan annotation, for Groovy ctx.'component-scan'(...) invocation.

Dependency Injection:

In xml based config, dependency injection can be done manually in xml, or by using annotations (@Autowire, @Required etc). On that case It is need to define <context:annotation-config/>

Question & Answer:

Q1: Why is the (so-called) Annotation Based Configuration actually using ClassPathXmlApplicationContext but not AnnotationConfigApplicationContext above?

Ans: It is a xml-based configuration with annotation-based bean definition.

Application Context:

  1. http://docs.spring.io/spring/docs/4.2.0.RELEASE/javadoc-api/org/springframework/context/ApplicationContext.html

AnnotationConfigApplicationContext:

1.AnnotationConfigApplicationContext and parent context

ClassPathXmlApplicationContext:

  1. http://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm
  2. http://www.mkyong.com/spring3/spring-3-hello-world-example/

Q2: The Java Based Configuration explained in the book seems like what should be called Annotation Based Configuration.?

Ans: You're right on that case. Java based configuration uses annotations, and called annotation based configuration. But annotation is a single part of Java, nothing else.

But elaborately we need to understand how this hierarchy comes from xml to annotation based and at last groovy based?

An alternative to XML setups is provided by annotation-based configuration which rely on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration. As mentioned in the section called “Example: The RequiredAnnotationBeanPostProcessor”, using a BeanPostProcessor in conjunction with annotations is a common means of extending the Spring IoC container. For example, Spring 2.0 introduced the possibility of enforcing required properties with the @Required annotation.

Spring 2.5 made it possible to follow that same general approach to drive Spring’s dependency injection. Essentially, the@Autowired annotation provides the same capabilities as described in Section 6.4.5, “Autowiring collaborators” but with more fine-grained control and wider applicability.

Spring 2.5 also added support for JSR-250 annotations such as @PostConstruct, and @PreDestroy.

Spring 3.0 added support for JSR-330 (Dependency Injection for Java) annotations contained in the javax.inject package such as @Inject and @Named. Details about those annotations can be found in the relevant section.

Q3: How many ways are there to configure Spring framework?

Ans:

Theoretically, 3 ways to describe configuration, and 2 ways to define beans. It turns 3*2 = 6 ways to configure Spring framework (by default). All of this ways can be used in combination with each other.

But Actually, in a single word, we can configure spring framework by using XML or annotations.