Spring IOC Pratap Kumar - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Spring

Framework
The Spring Framework provides a
comprehensive programming and
configuration model for modern
Java-based enterprise applications
• Open Source
• Light Weight
• Multi-Tier
• Modular
• Enterprise Application framework
• A key element of Spring is infrastructural support at the
application level: Spring focuses on the "plumbing" of
enterprise applications so that teams can focus on
application-level business logic, without unnecessary ties
to specific deployment environments.
More is
Better… I always believed there's a fine line between love and hate.

2
Spring
modules

3
Spring core
Container
Is an IOC Implementation
Inversion
of
IOC is design Principle, that
describes to have an external
Control?
entity to create and wire the
objects, Here the wiring is done by
using a concept DI.
“In object-oriented design, the
dependency inversion principle refers to
a specific form of decoupling software
modules.

Dependency
When following this principle, the
conventional dependency relationships

Injection?
established from high-level, policy-
setting modules to low-level,
dependency modules are reversed, thus
rendering high-level modules
independent of the low-level module
implementation details”
Source Wiki
Software
Requirement
Spring Tool Suite Java 8 +

7
Types of DI
Constructor Based Dependency Injection

Setter Based Dependency Injection


Spring
Container
Configuration
XML Based Configuration

Java Based Configuration


XML Based
Spring support XML Schema Definition to
Configuration
configure spring container using this style.

It can be useful to have bean definitions


span multiple XML files.
<beans>

XML Based
<bean>

<import>

<alias>
Configuration
</beans>
This allows to define a spring bean

<bean>
definition , This definition carry the
concrete information for the spring
container such that the container can
create and manage the spring bean.

Spring Bean ?
A Java Object created and/or managed by
the Spring container.
• A Spring IoC container manages one or more beans. These
beans are created with the configuration metadata that
you supply to the container (for example, in the form of
XML <bean/> definitions).
• Naming beans
• Instantiating beans
– By Executing Constructor
– By Executing Static Factory Method
– By Executing Non-Static Factory Method

Bean
Overview
13
<bean class=“…”>
Any one among this
<constructor-arg >

<value> <ref> <null>

<list> <set> <map>

Instantiation <array> <bean> <props>

with a </constructor-arg>

Constructor </bean>

14

<constructor-arg value=“” ref=“” type=“” name=“” index=“” >


• Constructor argument resolution matching occurs by
using the argument’s type. If no potential ambiguity exists
in the constructor arguments of a bean definition,

• the order in which the constructor arguments are defined


in a bean definition is the order in which those arguments
are supplied to the appropriate constructor when the bean
is being instantiated

Constructor • Constructor argument type matching

Argument
Resolution • Constructor argument index ,The index is zero-based.

• Constructor argument name 15


<bean class=“…” factory-method=“ …“

How to configure the arguments of the factory method?

Static Factory
Method
</bean>
16
<bean factory-bean=“…” factory-method=“ …“

How to configure the arguments of the factory method?

Non-Static
Factory
Method
</bean>
17
• Abstract bean Definition

• Bean Definition Inheritance

• Setter Base Dependency Injection

• Circular Dependency

• P Namespace spring 2.0

More • C Namespace spring 3.0

configuration • Util Namespace spring 2.0

18
• Depends on config
• Implement InitializingBean interface

• Using init-method attribute of <bean> tag

• Using JSR 250 annotation @PostConstruct

Spring Bean
Initialization
19
• Implement DisposableBean interface

• Using destroy-method attribute of <bean> tag

• Using JSR 250 annotation @PreDestroy

Spring Bean
Destruction
20
• singleton ( from 1.0 )

• prototype (from 1.0 )

• request ( from spring 2.0 )

• session

• application

• global session ( portlet )

• websocket ( new from spring 4.x )

Bean Scope • thread ( new from spring 3.0 )

• Scoped Bean Dependencies


21
• ApplicationContextAware

• ApplicationEventPublisherAware

• BeanClassLoaderAware

• BeanFactoryAware

• BeanNameAware

• EnvironmentAware

Aware
• MessageSourceAware

Interfaces
• ResourceLoaderAware

• ServletConfigAware

22
• ServletContextAware
• BeanPostProcessor

Container
Extension • BeanFactoryPostprocessor

Points
23
• Spring 2.0

– @Required

• spring 2.5

– @Autowired , @Value

– Jsr 250 Annotation

• @PostConstruct , @PreDestroy

Using
• @Resource

• Spring 3.0
Annotation – Jsr 330 Annotation
24
• @Named, @Inject , @Qualifier

• @Scope , @Singleton
• @Component and Further Stereotype Annotations

• Using Meta-annotations and Composed Annotations

• Automatically Detecting Classes and Registering Bean


Definitions

Classpath
Scanning and • Providing a Scope for Autodetected Components

Managed • Naming Autodetected Components

Components
• Providing Qualifier Metadata with Annotations
25
• @Bean & @Configuration
• @Bean Annotation
– Declaring a Bean
– Bean Dependencies
– Receiving Lifecycle Callbacks
– Specifying Bean Scope
– Customizing Bean Naming
– Bean Aliasing

Java Based
– Bean Description
• Instantiating the Spring Container by

Configuration
Using AnnotationConfigApplicationContext
– Simple Construction
– Building the Container Programmatically by Using 26
register(Class<?>…)
– Enabling Component Scanning with scan(String…)
• @Configuration Annotation
– Injecting Inter-bean Dependencies
– How Java-based Configuration Works Internally

• Composing Java-based Configurations


– Using the @Import Annotation
– Combining Java and XML Configuration
• @ImportResource("classpath:/com/acme/properties-
config.xml")

Java Based
Configuration
27

You might also like