6. Overview
Spring Framework is a powerful lightweight application development framework
used for Enterprise Java (JEE)
It provides everything you need to embrace the Java language in an
enterprise environment, with support for Groovy and Kotlin as alternative
languages on the JVM, and with the flexibility to create many kinds of
architectures depending on an application’s needs
Spring aims to help structure whole applications in a consistent, productive
manner, pulling together best-of-breed single-tier frameworks to create a
coherent architecture.
7. Goals
1. Simplicity: Spring Framework is simple because its non-invasive as it
uses POJO and POJI models.
2. Testability: Code split into modules make it easy to test.
3. Loosely coupling: Because Dependency Injection.
8. Goals
Lets understand this with an example:
public interface Bike {
public void start();
}
class Rider {
Bike b;
public void setBike(Bike b) {
this.b = b;
}
void ride() { b.start(); }
}
9. Goals
The Spring framework is a layered architecture which consists of several
modules, consists of features organized into about 20 modules.
All built on the top of its core container.
12. 1. The interface org.springframework.context.ApplicationContext represents the
Spring IoC container and is responsible for instantiating, configuring and
assembling objects known as beans, as well as managing their lifecycle.
The Spring framework provides several implementations of the ApplicationContext
interface — ClassPathXmlApplicationContext and FileSystemXmlApplicationContext
for standalone applications, and WebApplicationContext for web applications.
Spring Core
14. 1. DI: Dependency injection is a pattern through which to implement IoC, where the
control being inverted is the setting of object’s dependencies.
The act of connecting objects with other objects or “injecting” objects into
other objects, is done by an assembler rather than by the objects themselves.
Traditional programming: With DI:
Spring Core
15. 1. Exist in two major variants:
• Constructor injection: Examples by code/xml
• Setter injection: Examples by code/xml
Spring Core
16. 1. Spring Bean: Any object in the Spring framework that we initialize through Spring container
is called Spring Bean.
2. There are five scopes defined:
a. singleton (default) – Only one instance of the bean will be created for each container.
This is the default scope for the spring beans. While using this scope, make sure bean
doesn’t have shared instance variables otherwise it might lead to data inconsistency
issues.
b. prototype – A new instance will be created every time the bean is requested.
c. request – This is same as prototype scope, however it’s meant to be used for web
applications. A new instance of the bean will be created for each HTTP request.
d. session – A new bean will be created for each HTTP session by the container.
e. global-session – This is used to create global session beans for Portlet applications.
3. Spring Bean Configuration:
a. XML Based Configuration – By creating Spring Configuration XML file to configure the
beans. If you are using Spring MVC framework, the xml based configuration can be loaded
automatically by writing some boiler plate code in web.xml file.
b. Java Based Configuration – Starting from Spring 3.0, we can configure Spring beans using
java programs. Some important annotations used for java based configuration are
@Configuration, @ComponentScan and @Bean.
Spring Bean
18. Spring Expression Language
The Spring Expression Language (SpEL) is a powerful expression language that
supports querying and manipulating an object graph at runtime. It can be used with
XML or annotation-based Spring configurations.
There are several operators available in the language:
Type Operators
Arithmetic +, -, *, /, %, ^, div, mod
Relational <, >, ==, !=, <=, >=, lt, gt, eq, ne, le, ge
Logical and, or, not, &&, ||, !
Conditional ?:
Regex matches
Documents here
Project Example (Need to pull the root first, then follow guide line)
24. Spring Boot
1. Spring Boot makes it easy to create stand-alone, production-grade Spring based
Applications that you can "just run".
2. Features:
a. Create stand-alone Spring applications
b. Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
c. Provide opinionated 'starter' dependencies to simplify your build
configuration
d. Automatically configure Spring and 3rd party libraries whenever possible
e. Provide production-ready features such as metrics, health checks and
externalized configuration
f. Absolutely no code generation and no requirement for XML configuration
3. Examples:
a. Guideline
b. Project Example