SlideShare a Scribd company logo
Introduction to
Spring Framework and
Dependency Injection
What is a bean?
 Typical java bean with a unique id
 In spring there are basically two types
 Singleton
 One instance of the bean created and referenced each time it is requested
 Prototype (non-singleton)
 New bean created each time
 Same as new ClassName()
 Beans are normally created by Spring as late as possible
What is a bean definition?
 Defines a bean for Spring to manage
 Key attributes
 class (required): fully qualified java class name
 id: the unique identifier for this bean
 configuration: (singleton, init-method, etc.)
 constructor-arg: arguments to pass to the constructor at creation time
 property: arguments to pass to the bean setters at creation time
 Collaborators: other beans needed in this bean (a.k.a dependencies),
specified in property or constructor-arg
 Typically defined in an XML file
Sample bean definition
<bean id="exampleBean" class=”org.example.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo"><ref bean="yetAnotherBean"/></property>
<property name="integerProperty"><value>1</value></property>
</bean>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne; }
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo; }
public void setIntegerProperty(int i) {
this.i = i; }
}
What is a bean factory?
 Often seen as an ApplicationContext
 BeanFactory is not used directly often
 ApplicationContext is a complete superset of bean factory
methods
 Same interface implemented
 Offers a richer set of features
 Spring uses a BeanFactory to create, manage and locate
“beans” which are basically instances of a class
 Typical usage is an XML bean factory which allows configuration
via XML files
How are beans created?
 Beans are created in order based on the dependency graph
 Often they are created when the factory loads the definitions
 Can override this behavior in bean
<bean class=“className” lazy-init=“true” />
 You can also override this in the factory or context but this is not
recommended
 Spring will instantiate beans in the order required by their
dependencies
1. app scope singleton - eagerly instantiated at container startup
2. lazy dependency - created when dependent bean created
3. VERY lazy dependency - created when accessed in code
Bean properties?
 The primary method of dependency injection
 Can be another bean, value, collection, etc.
<bean id="exampleBean" class="org.example.ExampleBean">
<property name="anotherBean">
<ref bean="someOtherBean" />
</property>
</bean>
This can be written in shorthand as follows
<bean id="exampleBean" class="org.example.ExampleBean">
<property name="anotherBean" ref="someOtherBean" />
</bean>
Anonymous vs ID
 Beans that do not need to be referenced elsewhere can be defined
anonymously
 This bean is identified (has an id) and can be accessed to inject it into
another bean
<bean id="exampleBean" class="org.example.ExampleBean">
<property name="anotherBean" ref="someOtherBean" />
</bean>
This bean is anonymous (no id)
<bean class="org.example.ExampleBean">
<property name="anotherBean" ref="someOtherBean" />
</bean>
What is an inner bean?
<bean id="outer" class="org.example.SomeBean">
<property name="person">
<bean class="org.example.PersonImpl">
<property name="name"><value>Aaron</value></property>
<property name="age"><value>31</value></property>
</bean>
</property>
</bean>
It is a way to define a bean needed by
another bean in a shorthand way
• Always anonymous (id is ignored)
• Always prototype (non-singleton)
Bean init-method
The init method runs AFTER all bean dependencies are loaded
 Constructor loads when the bean is first instantiated
 Allows the programmer to execute code once all dependencies are present
<bean id="exampleBean" class=”org.example.ExampleBean"
init-method=”init” />
public class ExampleBean {
public void init() {
// do something
}
}
Bean values
 Spring can inject more than just other beans
 Values on beans can be of a few types
 Direct value (string, int, etc.)
 Collection (list, set, map, props)
 Bean
 Compound property
<bean class="org.example.ExampleBean">
<property name="email">
<value>azeckoski@gmail.com</value>
</property>
</bean>
Abstract (parent) beans
 Allows definition of part of a bean which can be reused many times in
other bean definitions
<bean id="abstractBean" abstract="true"
class="org.example.ParentBean">
<property name="name" value="parent-AZ"/>
<property name="age" value="31"/>
</bean>
<bean id="childBean"
class="org.example.ChildBean"
parent="abstractBean" init-method="init">
<property name="name" value="child-AZ"/>
</bean>
The parent bean defines 2 values
(name, age)
The child bean uses the parent age
value (31)
The child bean overrides the parent
name value (from parent-AZ to child-
AZ)
Parent bean could not be injected,
child could
Thanks!
http://www.springframework.org/

More Related Content

What's hot

Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
Vladislav sidlyarevich
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
NexThoughts Technologies
 
Hibernate architecture
Hibernate architectureHibernate architecture
Hibernate architecture
Anurag
 
Spring boot
Spring bootSpring boot
Spring boot
sdeeg
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
Anuj Singh Rajput
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
Spring Boot Interview Questions | Edureka
Spring Boot Interview Questions | EdurekaSpring Boot Interview Questions | Edureka
Spring Boot Interview Questions | Edureka
Edureka!
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
tola99
 
Spring ppt
Spring pptSpring ppt
Spring ppt
Mumbai Academisc
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Dineesha Suraweera
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Pei-Tang Huang
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
Alex Movila
 
Spring boot
Spring bootSpring boot
Spring boot
Gyanendra Yadav
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 

What's hot (20)

Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
Hibernate architecture
Hibernate architectureHibernate architecture
Hibernate architecture
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot Interview Questions | Edureka
Spring Boot Interview Questions | EdurekaSpring Boot Interview Questions | Edureka
Spring Boot Interview Questions | Edureka
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 

Similar to Spring beans

SpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.pptSpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.ppt
imjdabhinawpandey
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
ealio
 
Spring framework
Spring frameworkSpring framework
Spring frameworkAjit Koti
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
AnilKumar Etagowni
 
Spring by rj
Spring by rjSpring by rj
Spring
SpringSpring
Spring
s4al_com
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
s4al_com
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questionsDhiraj Champawat
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PawanMM
 
Spring framework IOC and Dependency Injection
Spring framework  IOC and Dependency InjectionSpring framework  IOC and Dependency Injection
Spring framework IOC and Dependency Injection
Anuj Singh Rajput
 
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Jsp session 6
Jsp   session 6Jsp   session 6
Jsp session 6
Anuj Singh Rajput
 
Spring 3.0 dependancy injection
Spring 3.0 dependancy injectionSpring 3.0 dependancy injection
Spring 3.0 dependancy injection
Rajiv Gupta
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
husnara mohammad
 
Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise-Spring framework 1
Skillwise-Spring framework 1
Skillwise Group
 
Spring introduction
Spring introductionSpring introduction
Spring introductionLê Hảo
 
introduction of Java beans
introduction of Java beansintroduction of Java beans
introduction of Java beans
shravan kumar upadhayay
 
Contexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platformContexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platformBozhidar Bozhanov
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
Vinay Kumar
 

Similar to Spring beans (20)

SpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.pptSpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.ppt
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
Spring by rj
Spring by rjSpring by rj
Spring by rj
 
Spring
SpringSpring
Spring
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
 
Spring framework IOC and Dependency Injection
Spring framework  IOC and Dependency InjectionSpring framework  IOC and Dependency Injection
Spring framework IOC and Dependency Injection
 
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
 
Using java beans(ii)
Using java beans(ii)Using java beans(ii)
Using java beans(ii)
 
Jsp session 6
Jsp   session 6Jsp   session 6
Jsp session 6
 
Spring 3.0 dependancy injection
Spring 3.0 dependancy injectionSpring 3.0 dependancy injection
Spring 3.0 dependancy injection
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
 
Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise-Spring framework 1
Skillwise-Spring framework 1
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
introduction of Java beans
introduction of Java beansintroduction of Java beans
introduction of Java beans
 
Contexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platformContexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platform
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
 

Recently uploaded

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 

Recently uploaded (20)

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 

Spring beans

  • 1. Introduction to Spring Framework and Dependency Injection
  • 2. What is a bean?  Typical java bean with a unique id  In spring there are basically two types  Singleton  One instance of the bean created and referenced each time it is requested  Prototype (non-singleton)  New bean created each time  Same as new ClassName()  Beans are normally created by Spring as late as possible
  • 3. What is a bean definition?  Defines a bean for Spring to manage  Key attributes  class (required): fully qualified java class name  id: the unique identifier for this bean  configuration: (singleton, init-method, etc.)  constructor-arg: arguments to pass to the constructor at creation time  property: arguments to pass to the bean setters at creation time  Collaborators: other beans needed in this bean (a.k.a dependencies), specified in property or constructor-arg  Typically defined in an XML file
  • 4. Sample bean definition <bean id="exampleBean" class=”org.example.ExampleBean"> <property name="beanOne"><ref bean="anotherExampleBean"/></property> <property name="beanTwo"><ref bean="yetAnotherBean"/></property> <property name="integerProperty"><value>1</value></property> </bean> public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public void setBeanOne(AnotherBean beanOne) { this.beanOne = beanOne; } public void setBeanTwo(YetAnotherBean beanTwo) { this.beanTwo = beanTwo; } public void setIntegerProperty(int i) { this.i = i; } }
  • 5. What is a bean factory?  Often seen as an ApplicationContext  BeanFactory is not used directly often  ApplicationContext is a complete superset of bean factory methods  Same interface implemented  Offers a richer set of features  Spring uses a BeanFactory to create, manage and locate “beans” which are basically instances of a class  Typical usage is an XML bean factory which allows configuration via XML files
  • 6. How are beans created?  Beans are created in order based on the dependency graph  Often they are created when the factory loads the definitions  Can override this behavior in bean <bean class=“className” lazy-init=“true” />  You can also override this in the factory or context but this is not recommended  Spring will instantiate beans in the order required by their dependencies 1. app scope singleton - eagerly instantiated at container startup 2. lazy dependency - created when dependent bean created 3. VERY lazy dependency - created when accessed in code
  • 7. Bean properties?  The primary method of dependency injection  Can be another bean, value, collection, etc. <bean id="exampleBean" class="org.example.ExampleBean"> <property name="anotherBean"> <ref bean="someOtherBean" /> </property> </bean> This can be written in shorthand as follows <bean id="exampleBean" class="org.example.ExampleBean"> <property name="anotherBean" ref="someOtherBean" /> </bean>
  • 8. Anonymous vs ID  Beans that do not need to be referenced elsewhere can be defined anonymously  This bean is identified (has an id) and can be accessed to inject it into another bean <bean id="exampleBean" class="org.example.ExampleBean"> <property name="anotherBean" ref="someOtherBean" /> </bean> This bean is anonymous (no id) <bean class="org.example.ExampleBean"> <property name="anotherBean" ref="someOtherBean" /> </bean>
  • 9. What is an inner bean? <bean id="outer" class="org.example.SomeBean"> <property name="person"> <bean class="org.example.PersonImpl"> <property name="name"><value>Aaron</value></property> <property name="age"><value>31</value></property> </bean> </property> </bean> It is a way to define a bean needed by another bean in a shorthand way • Always anonymous (id is ignored) • Always prototype (non-singleton)
  • 10. Bean init-method The init method runs AFTER all bean dependencies are loaded  Constructor loads when the bean is first instantiated  Allows the programmer to execute code once all dependencies are present <bean id="exampleBean" class=”org.example.ExampleBean" init-method=”init” /> public class ExampleBean { public void init() { // do something } }
  • 11. Bean values  Spring can inject more than just other beans  Values on beans can be of a few types  Direct value (string, int, etc.)  Collection (list, set, map, props)  Bean  Compound property <bean class="org.example.ExampleBean"> <property name="email"> <value>azeckoski@gmail.com</value> </property> </bean>
  • 12. Abstract (parent) beans  Allows definition of part of a bean which can be reused many times in other bean definitions <bean id="abstractBean" abstract="true" class="org.example.ParentBean"> <property name="name" value="parent-AZ"/> <property name="age" value="31"/> </bean> <bean id="childBean" class="org.example.ChildBean" parent="abstractBean" init-method="init"> <property name="name" value="child-AZ"/> </bean> The parent bean defines 2 values (name, age) The child bean uses the parent age value (31) The child bean overrides the parent name value (from parent-AZ to child- AZ) Parent bean could not be injected, child could