0% found this document useful (0 votes)
9 views23 pages

SpringContainer

Uploaded by

rajeshpushp21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views23 pages

SpringContainer

Uploaded by

rajeshpushp21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

SPRING CONTAINER

SPRING CONTAINER
• Spring Container is a program in Spring Framework that
manages the objects.
• The container gets its instructions by reading configuration
metadata to instantiate, configure, and assemble the objects.
• We can represent configuration metadata in different forms,
• XML, Java annotations, Java code
SPRING CONTAINER
• Spring Container has the following responsibilities:

• 1) Find and Scan Spring Beans

• 2) Create Objects for the beans that it found after scanning

• 3) Link/Inject objects

• 4) Destroy the objects


SPRING CONTAINER
• Types Of Spring Containers?
• 1) Bean Factory
• 2) ApplicationContext
• Bean Factory:
• It supports only XML configuration.
• It comes under package org.springframework.beans.
• Since we can’t create an object from an Interface in Java,
• The BeanFactory interface provides a most commonly used
implementation class which is XmlBeanFactory.
SPRING CONTAINER
• ApplicationContext:
• It supports all the forms of configuration: XML based, Java based
and Annotation based.

• It comes under package org.springframework.context.

• (A) ClassPathXmlApplicationContext : when configuration file is at


project’s class path.

• (B) FileSystemXmlApplicationContext : when configuration file is at


server’s file system.
SPRING CONTAINER
• (C) AnnotationConfigApplicationContext : when we are using
annotation-based configuration.

• Create a Spring Container:


• we can create a Spring container programmatically.
• ApplicationContext is the most widely used and BeanFactory is
rarely used, we will make use of ApplicationContext.
• ApplicationContext context = new
ClassPathXmlApplicationContext("com/dev/spring/example/confi
g.xml");
SPRING CONTAINER
• Using ClassPathXmlApplicationContext to create the container
context and supplying configuration file named as config.xml
(For XML-based configuration).

• AnnotationConfigApplicationContext context = new


AnnotationConfigApplicationContext(AppConfig.class);

• using AnnotationConfigApplicationContext to create the


container context and supplying configuration class named as
AppConfig.class (For Annotation-based configuration).
SPRING CONTAINER
• Get Bean from Spring Container?
• Spring Container (BeanFactory/ApplicationContext) provides
getBean() method to get bean from it.
• ApplicationContext ctx = new
ClassPathXmlApplicationContext("com/dev/spring/example/confi
g.xml");
• Person person = ctx.getBean(Person.class);
• System.out.println(person);
SPRING CONTAINER
• What is a Spring Bean?
• Spring Bean is a class in spring, which follows the rules
provided by the Spring Container. Below are the rules that
Spring Container expects from developers to follow while
creating a class that will act as a bean:
• 1) Class must be public.
• 2) Variables are recommended to be private.
• 3) Methods should be public.
• 4) Class should be under a package. It can be either under a
base package or its sub package.
SPRING CONTAINER
• 5) Provide any one of the two combinations given below.
• (A) Default constructor with setter & getter methods
• (B) Parameterized constructor
• 6) Class can override 3 methods from Object class: toString(),
hashcode() and equals().
• 7) Class can implement java.io.Serializable interface.
SPRING CONTAINER
• What inputs a Spring container requires from a Programmer to
create Objects?
• Spring container requires two inputs from a programmer to create
an object:
• Spring Bean & Spring Configuration.
• Spring Bean is a specific type of class that follows rules provided
by the
Spring Container.
• Spring configuration generally contains the name of the bean,
linking details
with other beans and some other information.
• Spring Configuration is also known as Configuration metadata.
SPRING CONTAINER
• Spring Configuration can be in any of the three forms:
• 1) XML-based Configuration
• 2) Java-based Configuration
• 3) Annotation-based Configuration
• Please note that annotation configuration is the most popular
and highly used in the industry. Spring Boot has already
removed the XML configuration.
SPRING CONTAINER
• Different ways to provide Configuration Metadata?
• There are three ways of Configuration metadata that a
developer provides to Spring Container.
• Developer need to create Spring configuration metadata to tell
Spring container how to initialize, configure, wire and assemble
the application specific beans.
SPRING CONTAINER
• XML-based Configuration:
• In Spring Framework, developer defines the bean
name, dependencies and the other services needed by Spring
Container.
• If these details are specified in configuration files which are in
XML format, it is known as XML-based configuration.
• They generally start with a bean tag.
SPRING CONTAINER
• <bean id="employeeBean"
class="com.dev.spring.EmployeeBean">
• <property name="name" value="employee1"/>
• <property name="age" value="24"/>
• </bean>
• Annotation-based Configuration:
• Spring 2.5 introduces annotation-based configuration.
By default, annotation wiring is not turned on in the
Spring framework. So, in order to use annotation-based
configuration, we need to enable it before using it by specifying
<context:annotation-config/> tag. Once this tag is configured,
we can start annotating our code.
SPRING CONTAINER
• <beans>
• <context:annotation-config/>
• </beans>

• Spring Boot has reduced the use of XML configurations, We


can directly use annotations whenever it is required.
• To make a class as a bean, we need to apply an annotation on
the top of the class.
SPRING CONTAINER
• There are various annotations that we can apply on top of the
class,
such as @Component, @Controller, @Service, @Repository etc.
• Most of the time we apply @Component annotation as it is the
basic annotation. Other annotations have some specific purpose.
SPRING CONTAINER
• @Component
• public class Employee{
• private Long id;
• private String name;
• private String department;
• }
• @Component annotated classes and creates bean definitions,
• considering that this class acts as a spring bean.
SPRING CONTAINER
• Java-based Configuration:
• Starting with Spring 3.0, a pure-Java configuration container was
provided.
• We don’t need any XML with this method of configuration.
• The key features in Spring Framework’s new Java-configuration
support are @Configuration annotated classes and @Bean
annotated methods.
• Using @Configuration annotation represents that Spring
• container will use it as a source of Beans definitions.
• @Bean tells Spring container that method will return an object
• which should be registered as a bean in Spring application
context.
SPRING CONTAINER
• 1. @Bean annotation plays the same role as the <bean/>
element.
• 2. @Configuration class

• @Configuration
• public class EmployeeConfig{
• @Bean
• public EmployeeBean permanentEmployee(){
• return new EmployeeBean();
• }
SPRING CONTAINER
• @Bean(name="contractEmployee")
• public EmployeeBean otherEmployee(){
• return new EmployeeBean();
• }
•}
• Method name becomes the bean id by default. we can provide
the name of bean as per our choice by specifying ‘name’
attribute of @Bean annotation .
SPRING CONTAINER
• When to use BeanFactory container vs.
ApplicationContextContainer?
• If you are using small scale, light weight spring based
application, prefer using BeanFactory container as it takes less
memory to work.
• If there is a memory limitation in place, prefer using
BeanFactory container. For example, mobile apps, embedded
system apps, IOT apps etc.
• In all other heavy weight apps where memory limitation is not in
place, such as web apps, enterprise apps, distributed apps,
desktop apps etc., prefer using ApplicationContext container.
• We should prefer using ApplicationContext container wherever it
is possible to use.
THANK YOU

You might also like