Terminologies used in Spring Framework
Tight Coupling, Loose Coupling, Dependency Injection, IOC Container, Application Context, Spring
Bean,Auto Wiring
Component Scan
Point 1:
@ Configuration – Is used to create one or more than one Beans at runtime by using
@ Bean annotation . – use same bean name in application context when retrieving the bean by using
getBean method .
Here we can customized Bean name by mentioned name attribute with annotation
@Bean(name=”whatever”)
To call it we will use
var context= new AnnotationConfigApplicationContext(configurationClassname.class);
context.getBean(“beanName”);
Point 2 :
In Java 16 we have feature “record” which will create Pojo Bean and we don’t need to create setter or
getter as this will automatically.
Use above of class
Ex. record Person(String name, int age) {};
Point3:
@configuration is kind @Component but not vice versa.
Spring Container – Is also called Spring Context or IOC container which managed the spring beans and
their life cycles .It takes classes (Pojo , Configuration ) as input and creates a running system.
There are two popular IOC containers:
Bean Factory- This is core Spring IOC Container. Basic Spring Container
ApplicationContext : it extended version of spring Bean Factory .Its advance container with enterprise-
specific feature.
Easy to use in Web application .
Easy Internationalization
Easy Integration with Spring AOP
Java Bean Vs POJO Vs Spring Bean:
Java Bean (Using in EJB which nowdays rare to use )-
It contains non-arg constructor,
Getter/setter method of fields
Implements Serializable interface
class JavaBean implements Serializable{
private int num;
JavaBean(){
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
PoJo – Plain old java object
Every class is POJO in java.
class Pojo
{
int number ;
String Text;
public String toString() {
return number + " "+Text;
}
}
Spring Bean – Bean which managed by Spring Container.
Point 4 – If you have multiple Beans for same class then we use
@Primary to solve the problem
(Ex – Issue - No qualifying bean of type 'com.in28minutes.learnspringframework.Person'
available: expected single matching bean but found 3:
person2,person2MethodCall,person2ParameterPass
)
Also Can use
@Qualifier to simulate which Bean spring container will called if multi matching Beans.
If we need to create objects and ask Spring to create Bean for us then we use @Component- Bean
creates by Spring Container(instance of class which is managed by Spring framework)
@ComponentScan (“package name”)– Scan the package where it can find the class (Bean or component
) for which spring would create the object.
If we don’t defined it will check in same package
@Qualifier (“name of bean”)- When SPECIFIC Bean should be auto-wired (name of bean can be used
as Qualifier)
@Primary- A bean should be given preference when multiple candidates are qualified. Specific Bean
should be auto-wired.
Primary vs Qualifier-
Primary-When need to give preference of Bean then use Primary and use just @Autowired annotation
Qualifier-When need to use specific Bean then use @Autowired +@ Qualifier.
@Qaulifer is higher priority than @Primary
Dependency Injection types:
Constructor Based- Dependencies are set by creating Bean using its constructor(@ Autowired is not
mandatory on this and recommended )
Setter Based- Dependencies are set by calling setter methods on your beans.(@ Autowired on setter
methods)
Field– No setter or constructor – Dependencies are set using reflection. (Using @Autowired on field)
@Component
class MyBusinessClass{
@Autowired
Dependecies1 s1;
@Autowired
Dependencies2 s2;
}
Dependency (Example – GammingRunner Class needs GamingConsoleImple)
Dependency Injections – Identify the beans , their dependencies and wire them together (it will also
called Inversion of Control - IOC)
Autowiring - Process of wiring in dependencies for a Spring Bean
Comparision Between @Comnponent and @ Bean
Heading @Component @Bean
Where Can be used in any java classes Typically, it is used on metho
ds in Configuration classes.
Ease of use Very easy to use. Just need to Need to write all code
write annotation
Autowiring Yes- Constructor, Setter or Field Yes- method call or method
parameter
Who creates Beans? Spring Framework You write Bean creation code
Recommended for Instantiating Beans for your own 1. Custom Business
application code: @Component Logic
2. Instantiating Bean for
3 rd. party libraries:
@Bean
Initialization of Spring Bean-
Default initialization of Spring Bean is Eager.(Means Spring Container initialized bean at the time of
running while this is not in use )
@Lazy – to make sure that it will initialize when it is required.
It can be used almost every where @component and @ Bean are used.
Lazy-resolution proxy will be injected in place of actual dependency.
Can be used on Configuration class (@Configuration)- all @Bean methods within Configuration
will be lazy initialized.
Eager Initialization is recommended because
1. Error in configuration is discovered immediately at the time of application start up.
You can configure Lazy Initialization using Lazy annotation (Not recommended and not frequently used)
Comparision bwt Lazy and Eager Initialization
Heading Lazy Initialization Eager Initialization
Initialization Time Bean is initialized when it is Bean Initialized at the start
made first use of in the up of the application
application
Default Not Default Default
Code snippet @Lazy or @Lazy(value=true) @Lazy(value=false) or
(absence of @Lazy )
Usage Rarely use Frequently used
Memory use Less as initialized when it is All beans will be
required initialized at the time
of starting up of the
application
Recommended scenarios When bean very rarely used in Most of your beans
application
Scope of Bean-
@Scope(ConfigurableBeanFactory.type of scope)
Prototype – Each time we will get new Bean instance . Many instancse per IOC container
Singlton- Every time will get same bean instance (Its default scope).Only one instance per IOC container
Scope available only for web-aware Spring Application Context
Request- one object instance per single HTTP request
Session- one object instance per user HTTP session
Application- one object instance per web application runtime
WebSocket- one object instance per WebSocket instance
Java Singlton vs Spring Singlton –
Spring Singelton – One object instance as per IOC container (May be chance that JVM is using multiple
IOC container then more than one will available)
Java Singleton (GOF) – One Object Instance as per JVM
Else both are focus on single object
Singleton Vs Prototye scope
Heading Prototype Singleton
Instance Many instances per IOC container One instance per IOC container
Bean New Bean instance created every Same bean instance reused
time the bean referred to
Code snippet @Scope(value=ConfigurableBeanF @Scope(value=ConfigurableBeanFact
actory.SCOPE_PROTOTYPE) ory.SCOPE_SINGLETON) or Default
Default Not Default
Usage Rarely used Frequently
Recommende Stateful Bean Stateless Bean
d scenarios
@PostConstruct – Its called after dependency injected is ready to initialize.
@PreDestroy- to clean up before destroying the object
For CDI implementation in Jakarta EE
Need to add following dependency :
Group id- Jakarta.inject
artifact id- Jakarta.inject-api
Here @Named is alternation of @Component
@Inject is replacement of @Autowired in spring framework
Whenever we need to pass something from controller then we will use Model.
For sending Parameter we will use @RequestParam
3 Important Changes
Change 01: Use jakarta.servlet.jsp.jstl instead of glassfish-jstl
Change 02: Use jakarta.tags.core instead
of http://java.sun.com/jsp/jstl/core as taglib
Change 03: Run mvn clean install to update libraries
Details: https://github.com/in28minutes/master-spring-and-spring-boot/blob/main/spring-boot-
3.2.x-changes.md
Change 01: Use jakarta.servlet.jsp.jstl instead of glassfish-
jstl
Spring Boot 3.2.x and greater
1. <dependency>
2. <groupId>org.glassfish.web</groupId>
3. <artifactId>jakarta.servlet.jsp.jstl</artifactId>
4. </dependency>
jakarta.servlet.jsp.jstl replaces glassfish-jstl (recommended for Spring
Boot <= 3.1.x)
1. <dependency>
2. <groupId>org.eclipse.jetty</groupId>
3. <artifactId>glassfish-jstl</artifactId>
4. </dependency>
jakarta.tags.core instead
Change 02: Use
of http://java.sun.com/jsp/jstl/core as taglib
Spring Boot 3.2.X and greater
Use
1. <%@ taglib prefix="c" uri="jakarta.tags.core" %>
Spring Boot 3.1.X and lower
Use
1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Change 03: Run mvn clean install to update libraries
And then execute the mvn clean install command
Thank you!
in28minutes team (Maahi and Ranga)