Spring Framework

1. Dependency Injection(DI) vs Inversion of Control (IoC)

Answer: https://stackoverflow.com/a/49447249/231567

The diagram says it all.

2. Spring Framework vs Spring Boot?

Spring Framework is mostly a DI framework with nice support for other frameworks such as :

a. JDBC (JDBC Template)

b. JMS

c. Transaction management

d. Hibernate/JPA

e. AOP/Testing/Security/etc

The DI helps in making the application maintainable as well as Testable.

Spring boot is another layer over Spring Framework which add:

1. Opinionated ‘starter' dependencies to simplify the build and application configuration

E.g Springbootstarter-web brings embedded tomcat, spring framework (core, beans, context),logback/slf4j, Jackson, etc.

2. Metrics, Health check, and externalized configuration

3. Automatic config for Spring functionality(Auto Configuration) – whenever possible.

E.g. If Hibernate is available, and datasource is not there. I will configure datasource. If DB2 is there, I will create DB2 datasource. I will create In Memory otherwise.

How does Spring Boot autoconfiguration work?

It is all based on interface @Conditional. It creates (auto configures) a lot of beans based on conditions mentioned in @Conditional, that has many flavours such as @ConditionalOnBean, @ConditionalOnClass, etc.

https://www.marcobehler.com/guides/spring-boot

3. What is @SpringBootApplication annotation?

It is a combination of @Configuration, @EnableAutoConfiguiration, @ComponentScan

4. What does SpringApplicaiton.run method does:

Do the following in this order:

a. Identify application Type (Servlet/Application/Reactive)

b. Create application Context.

c. Register all the bean with Annotations, e.g. beans with @Component, @Bean to the context.

d. Start AutoConfiguration

5. What is @Configuration?

It is the replacement of XML. When a class is annotated as @Configuration, it is a special class that initializes beans, etc.

6. What is @Component?

Register the class and create a bean (singleton or prototype).

ApplicationContext vs BeanFactory:

2 different implementation of IoC in Spring. Use application context whenever possible by default. Beanfactory is for critical memory consumption applications e.g. it does not initializes beans at the start.

"Short version: use an ApplicationContext unless you have a really good reason for not doing so."

https://docs.spring.io/spring-framework/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory

Bean Factory

    • Bean instantiation/wiring

Application Context

    • Bean instantiation/wiring

    • Automatic BeanPostProcessor registration

    • Automatic BeanFactoryPostProcessor registration

    • Convenient MessageSource access (for i18n)

    • ApplicationEvent publication

@Component vs @Bean

https://stackoverflow.com/a/40861225/231567

@ComponentScan

This scans all the components. The component is the parent of the stereotypes such as Service, Repository, Controller. So, everything of this type is scanned.

Difference between @Component (that is the parent) and others such as @Service, @Repository, @Controller.

No difference. They are just markers to tell the difference between the components.

What does the annotation mean in Spring?

Annotations are processed at runtime. Additional code is written to interpret and process the annotations.

Spring boot has a lot of annotations. How to understand the flow of code and how to understand what annotation does (where to look for code)?

Looks like the annotation code is not executed in sequence. Therefore it may not be easy to understand the flow. The annotation code is not easy to follow as well.