Friday, December 1, 2017

How many ways are there to configure the Spring framework? What are the differences between them technically?

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 verbositypoor 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:
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.

No comments:

Post a Comment