Spring IOC Pratap Kumar - 1
Spring IOC Pratap Kumar - 1
Spring IOC Pratap Kumar - 1
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
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 >
with a </constructor-arg>
Constructor </bean>
14
Argument
Resolution • Constructor argument index ,The index is zero-based.
Static Factory
Method
</bean>
16
<bean factory-bean=“…” factory-method=“ …“
Non-Static
Factory
Method
</bean>
17
• Abstract bean Definition
• Circular Dependency
18
• Depends on config
• Implement InitializingBean interface
Spring Bean
Initialization
19
• Implement DisposableBean interface
Spring Bean
Destruction
20
• singleton ( from 1.0 )
• session
• application
• 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
• @PostConstruct , @PreDestroy
Using
• @Resource
• Spring 3.0
Annotation – Jsr 330 Annotation
24
• @Named, @Inject , @Qualifier
• @Scope , @Singleton
• @Component and Further Stereotype Annotations
Classpath
Scanning and • Providing a Scope for 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
Java Based
Configuration
27