Spring
Spring
Spring
Our Java code is tightly coupled with new keyword to create the object so dependency injection will
help us to remove dependency of creating object from Java and Spring will be responsible for injecting
the dependency.
SETTER DEPENDENCY INJECTION (SDI): This is the simpler of the two DI methods. In this, the DI will be
injected with the help of setter and/or getter methods
CONSTRUCTOR DEPENDENCY INJECTION (CDI): In this, the DI will be injected with the help
of constructors
Constructor DI will provide 100% DI as we need to pass all parameters of constructor otherwise it will
be compile time error .
Constructor(Property prop)
{ this.prop = prop;
Spring IoC Container is the core of Spring Framework. It creates the objects, configures and assembles
their dependencies, manages their entire life cycle.
BeanFactory and the ApplicationContext interfaces acts as the IoC container. The ApplicationContext
interface is built on top of the BeanFactory interface. It adds some extra functionality than BeanFactory
such as simple integration with Spring's AOP, message resource handling (for I18N), event propagation,
application layer specific context (e.g. WebApplicationContext) for web application. So it is better to use
ApplicationContext than BeanFactory.
1. singleton This scopes the bean definition to a single instance per Spring IoC container (default).
2. prototype This scopes a single bean definition to have any number of object instances.
3 request This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware
Spring ApplicationContext.
4 session This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware
Spring ApplicationContext.
5 global-session This scopes a bean definition to a global HTTP session. Only valid in the context of a
web-aware Spring ApplicationContext.
@QUALIFER ANNOTATION: When we have multiple beans of same type then autowiring will not work
and it will throw and exception NoSuchBeanDefinitionException as by default autowiring work on
byType only.
To resolve this we can use @Qualifer annotation and pass the particular bean name as parameter.
@PRIMARY ANNOTATION: it is used to give high preference to the specific bean among multiple beans
of same type to inject to a bean.
For example if we have two datasource bean and we want to give priority to datasource 1 then we can
use this on top of datasource1 bean
Like If we want create a bean on some condition then we can use this annotation .
It allows you to load beans conditionally depending on a certain environment property or configuration
of a property. By default, any property that exists and is not equal to false is matched.
@PROFILE ANNOTATION: Like if we have multiple profile like dev , qa , prod we can use @Profile to load
any specific environment.
A profile is a set of configuration settings. Spring Boot allows to define profile specific property files in
the form of application-{profile}. properties . It automatically loads the properties in an application.
@CONFIGURATIONPROPERTIES ANNOTATION: if we want to read yml file then we can use this
annotation and it will allows to map the entire Properties and Yaml files into an object easily