Spring
Spring
Java Spring
Build the apps that make the world run
Spring dominates the Java ecosystem with 60% using it fortheir main
applications
● OOPS Recap
● Introduction to Spring
Framework
Course ● Basics of Spring
● Spring Event Handling
Contents ● Aspect Oriented
Programming
● Spring Boot
Getting The Basics Right
● Object
● Class
● Data Abstraction
● Inheritance
● Data Encapsulation
● Polymorphism
● Dynamic Binding
● Message Passing
Class Vs Object
Class Object
● Blueprint for creating objects ● Instance of a class
● Does not get memory when it is ● Gets memory when they are
created created
void makeSound();public void void makeSound() { public static void main(String[] args) {
}
Data Encapsulation
● Inheritance
● Polymorphism
● Dynamic Binding
● Message Passing
Dependency Injection vs Inversion of Control
SpringContext.xml
< bean id="tyre1Bean" class="com.tg.Tyres"> <bean id="InjectwithSetter"
<property name="name" value="MRF"></property> class="com.tg.Vehicle">
<property name="place" value="India"></property> <property name="engine" ref="ToyotaBean">
<property name="message" value="Make in India"> </property>
</property> <property name="tyre" ref="tyre1Bean">
</ bean> </property>
<bean id="ToyotaBean" class="com.tg.ToyotaEngine"> </ bean>
<property name="company" value="Toyota"> <bean id="InjectwithConstructor"
</property> class="com.tg.Vehicle">
<property name="cost" value="300000.00"> <constructor - arg name="engine"
</property> ref="ToyotaBean">
</ bean> </ constructor - arg>
< bean id="tyre2Bean" class="com.tg.Tyres"> <constructor - arg name="tyre"
<property name="name" value="TVS"></property> ref="tyre2Bean">
<property name="place" value="India"></property> </ constructor - arg>
<property name="message" value="Make in India"> </ bean>
</property>
</ bean>
Spring framework architecture
Core Container
● The Core module provides the fundamental parts of the framework, including the IoC
and Dependency Injection features.
● The Bean module provides BeanFactory, which is a sophisticated implementation of
the factory pattern.
● The Context module builds on the solid base provided by the Core and Beans modules
and it is a medium to access any objects defined and configured. The
ApplicationContext interface is the focal point of the Context module.
● The SpEL module provides a powerful expression language for querying and
manipulating an object graph at runtime.
Data Access and Integration
● The JDBC module provides a JDBC-abstraction layer that removes the need for
tedious JDBC related coding.
● The ORM module provides integration layers for popular object-relational mapping
APIs, including JPA, JDO, Hibernate, and iBatis.
● The OXM module provides an abstraction layer that supports Object/XML mapping
implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
● The Java Messaging Service JMS module contains features for producing and
consuming messages.
● The Transaction module supports programmatic and declarative transaction
management for classes that implement special interfaces and for all your POJOs.
Web
● The Web module provides basic web-oriented integration features such as multipart
file-upload functionality and the initialization of the IoC container using servlet
listeners and a web-oriented application context.
● The Web-MVC module contains Spring's Model-View-Controller (MVC)
implementation for web applications.
● The Web-Socket module provides support for WebSocket-based, two-way
communication between the client and the server in web applications.
● The Web-Portlet module provides the MVC implementation to be used in a portlet
environment and mirrors the functionality of Web-Servlet module.
Miscellaneous
● The objects that form the backbone of your application and that are managed bythe
Spring IoC container are called beans.
● A bean is an object that is instantiated, assembled, and otherwise managed by aSpring
IoC container.
● These beans are created with the configuration metadata that you supply to thecontainer.
● Bean definition contains the information called configuration metadata, which isneeded
for the container to know the following −
● How to create a bean
● Bean's lifecycle details
● Bean's dependencies
Spring Beans Properties
● class: This attribute is mandatory and specifies the bean class to be used to create thebean.
● name: This attribute specifies the bean identifier uniquely. In XMLbased configuration
metadata, you use the id and/or name attributes to specify the bean identifier(s).
● scope: This attribute specifies the scope of the objects created from a particular beandefinition
● constructor-arg: This is used to inject the dependencies
● properties: This is used to inject the dependencies
● autowiring mode: This is used to inject the dependencies
● lazy-initialization mode: A lazy-initialized bean tells the IoC container to create a beaninstance
when it is first requested, rather than at the startup
● initialization method: A callback to be called just after all necessary properties on thebean
have been set by the container.
● destruction method: A callback to be used when the container containing the bean is
destroyed.
Spring Beans Scope
● singleton: This scopes the bean definition to a single instance per Spring IoC container(default).
● prototype: This scopes a single bean definition to have any number of object instances.
● request: This scopes a bean definition to an HTTP request. Only valid in the context of aweb-
aware Spring ApplicationContext.
● session: This scopes a bean definition to an HTTP session. Only valid in the context of aweb-
aware Spring ApplicationContext.
● global-session: This scopes a bean definition to a global HTTP session. Only valid in thecontext
of a web-aware Spring ApplicationContext.
Spring Bean Lifecycle
Spring Bean Lifecycle Callback
● Setup JDK
● Setup IDE (Intellij, Java)
● Setup spring libraries using maven/gradle or manually downloading the libraries
Creating bean using factory bean
Spring provides a way to automatically detect the relationships between various beans. This can be done by
declaring all the bean dependencies in Spring configuration file. So, Spring is able to utilize the
BeanFactory to know the dependencies across all the used beans.
● byType: The byType mode injects the object dependency according to type. So it can have adifferent
property and bean name. It internally calls the setter method.
<bean id="car" class="com.example.Car" autowire="byType"/>
● constructor: Spring matches constructor parameters with beans by type.
<bean id="car" class="com.example.Car" autowire="constructor"/>
● autodetect: Deprecated from version 3.0+
● Annotation-based Autowiring:
● @Autowired -- Automatically injects a bean by type
@Component
public class Car
{
@Autowired
private Engine engine;
}
Spring bean autowire and its types[Contd.]
Example:
Example:
https://examples.javacodegeeks.com/enterprise-java/spring/20-spring-framework-best-practices/
Spring java configuration vs xml
● XML configurations can take a lot of work when there are a lot of beans, while annotations
minimize the XML configurations.
● Annotation injection is performed before XML injection. Thus, XML configurations will
override that of the annotations.
Spring Event Handling
Spring supports the @AspectJ annotation style approach and the schema-based approach toimplement
custom aspects.
● XML Schema based: Aspects are implemented using the regular classes along with XMLbased
configuration.
● @AspectJ based: @AspectJ refers to a style of declaring aspects as regular Javaclasses
annotated with Java 5 annotations.
Aspect Oriented Programming - Example
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop
https://www.digitalocean.com/community/tutorials/spring-aop-example-tutorial-aspect-advice-pointcut-joinp oint-annotations