Spring

You might also like

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

Spring

Inversion of Control
Inversion of Control is a principle in software engineering which transfers the control of
objects or portions of a program to a container or framework. We most often use it in the
context of object-oriented programming.

Dependency injection is a pattern we can use to implement IoC, where the control being
inverted is setting an object’s dependencies.

@Autowired
We can use the @Autowired to mark a dependency which Spring is going to resolve and
inject. We can use this annotation with a constructor, setter, or field injection.

@Qualifier
We use @Qualifier along with @Autowired to provide the bean id or bean name we want to
use in ambiguous situations.

@Required
@Required on setter methods to mark dependencies that we want to populate through XM.

@Value
We can use @Value for injecting property values into beans. It’s compatible with
constructor, setter, and field injection.

@DependsOn
We can use this annotation to make Spring initialize other beans before the annotated one.
Usually, this behavior is automatic, based on the explicit dependencies between beans.

@Lazy
We use @Lazy when we want to initialize our bean lazily. By default, Spring creates all
singleton beans eagerly at the startup/bootstrapping of the application context.

@Lookup
A method annotated with @Lookup tells Spring to return an instance of the method’s return
type when we invoke it.

@Primary
Sometimes we need to define multiple beans of the same type. In these cases, the injection
will be unsuccessful because Spring has no clue which bean we need.

@Scope
We use @Scope to define the scope of a @Component class or a @Bean definition. It can
be either singleton, prototype, request, session, globalSession or some custom scope.
singleton (Default) Scopes a single bean definition to a single object instance for each
Spring IoC container.

prototype Scopes a single bean definition to any number of object instances.

request Scopes a single bean definition to the lifecycle of a single HTTP request. That
is, each HTTP request has its own instance of a bean created off the back of
a single bean definition. Only valid in the context of a web-aware Spring
ApplicationContext.

session Scopes a single bean definition to the lifecycle of an HTTP Session. Only
valid in the context of a web-aware Spring ApplicationContext.

applicatio Scopes a single bean definition to the lifecycle of a ServletContext. Only


n valid in the context of a web-aware Spring ApplicationContext.

websock Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in
et the context of a web-aware Spring ApplicationContext.

@Profile
If we want Spring to use a @Component class or a @Bean method only when a specific
profile is active, we can mark it with @Profile. We can configure the name of the profile with
the value argument of the annotation:

You might also like