0% found this document useful (0 votes)
22 views2 pages

Spring

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

WHAT IS DEPENDENCY INJECTION , TYPES OF DI , WHICH IS BETTER AND WHY ?

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.

DI will invert control of creating of object from Java to Spring

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

 // Setter method for property prop

    public void setProp(Property prop)


    {
        this.prop = prop;
    }

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 to set the CDI

Constructor(Property prop)

{ this.prop = prop;

EXPLAIN IOC ? WHAT ARE TYPES OF IOC CONTAINER IN SPRING ?

Spring IoC Container is the core of Spring Framework. It creates the objects, configures and assembles
their dependencies, manages their entire life cycle.

There are two types of IoC containers. They are: BeanFactory. ApplicationContext.

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.

SterioTypes in Spring : @Service ,@Repository ,@Component,@Controller


WHAT ALL SCOPE OF BEAN AVAILABLE IN SPRING?

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

@CONDITIONAL ANNOTATION: It supports the “if-then-else ” conditional checking for bean


registration.

Like If we want create a bean on some condition then we can use this annotation .

@CONDITIONALONPROPERTY ANNOTATION: This annotation is used to conditionally create a Spring


bean depending on the configuration of a property

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

You might also like