Demystifying Spring Internals

VMware Tanzu
VMware TanzuVMware Tanzu
│©2022 VMware, Inc.
Demystifying Spring
Internals
Madhura Bhave
🤔
It's just
annotations... how
hard can it be?
to the code...
@ComponentScan
@Component Scan
@Component Scan
@Controller
@Service @Repository
@RestController
🧐
Scan
🧐
class HelloUtils
@Service
class HelloService
@RestController
class HelloController
class MyOtherUtils
💩
💩
Scan
🧐
class HelloUtils
@Service
class HelloService
@RestController
class
class MyOtherUtils
💩
💩
HelloController
@RestController
class HelloController
HelloController
📒
✍
Bean Instance
Bean De
fi
nition
(makes)
📒
✍
Bean De
fi
nition
BeanDefinition
•Lazy
•Primary
•Scope
•Type
•Factory Method Name
•Init Method Name
•Destroy Method Name
📒
✍
📒
✍
📒
✍
📒
✍
📒
✍
📒
✍
📒
✍
📒
✍
📒
✍
📒
✍
BeanFactory
BeanDefinitionRegistry
↪
↩
Let me get beans
Let me register bean definitions
BeanFactory
BeanDefinitionRegistry
↪
↩
Let me get beans
Let me register bean definitions
BeanFactory BeanDefinitionRegistry
DefaultListableBeanFactory
Let me get beans Let me register bean definitions
DefaultListableBeanFactory
•The 💚 of Spring
•Manages Bean De
fi
nitions
•Manages Bean Instances
to the code...
BeanFactory
ApplicationContext
spring-beans.jar
spring-context.jar
Environment
MessageSource
ApplicationEvent
@Configuration
ApplicationContext
AbstractApplicationContext
GenericApplicationContext
AnnotationConfigApplicationContext
Provides @Configuration support
Contains a DefaultListableBeanFactory
Basic implementation that detects special Beans
to the code...
AnnotationConfigApplicationContext
@Configuration
class ApplicationConfiguration {
@Bean
public HelloMessageProvider messageProvider() {
return new HelloMessageProvider();
}
}
This is also a @Component
Turns the method signature into a BeanDefinition
Invoked when the bean instance is needed
📒
✍ type = HelloMessageProvider
factoryBean = "applicationCon
fi
guration"
factoryMethodName = "messageProvider"
📒
✍ type = HelloMessageProvider
factoryBean = "applicationCon
fi
guration"
factoryMethodName = "messageProvider"
📒
✍ type = HelloController
👋 Get me the HelloController Bean
🧐 OK, let me get that BeanDe
fi
nition
🤔 Wait... its constructor needs a HelloMessageProvider
🧐 OK, let me get that BeanDe
fi
nition
🤔 Wait it needs an 'applicationCon
fi
guration' factory bean
📒
✍ type = ApplicationCon
fi
guration
🧐 <sigh> OK, let me get that BeanDe
fi
nition
🥳 Create ApplicationCon
fi
guration Bean instance
🥳 Invoke messageProvider() method to create
HelloMessageProvider Bean instance
🥳 Create HelloController Bean instance
injecting HelloMessageProvider
DefaultListableBeanFactory
AnnotationConfigApplicationContext
AnnotationConfigApplicationContext
(uses) ConfigurationClassPostProcessor
BeanDefinitionRegistryPostProcessor
BeanFactoryPostProcessor
to the code...
GraalVM
🧐
🤷
main
SpringApplication.run
@ComponentScan @Configuration @Bean
🤔
🛏
💩
💡
🩹Add re
fl
ection hints
Write better code
😎
🤨
Make this... @Configuration
class ApplicationConfiguration {
@Bean
public HelloMessageProvider messageProvider() {
return new HelloMessageProvider();
}
}
Into something more like this...
BeanDefinition bd = new RootBeanDefinition(HelloMessageProvider.class,
HelloMessageProvider::new);
registry.registerBeanDefinition("helloMessageProvider", bd);
🤔
It's just
annotations... how
hard can it be?
💡
📒
✍
📒
✍
📒
✍
📒
✍ 📒
✍
📒
✍
📒
✍
📦
🧐 Run the app so we have bean de
fi
nitions
📒
✍
📒
✍
📒
✍
DefaultListableBeanFactory
AnnotationConfigApplicationContext
✋ Stop before we create the bean instances
📒
✍
😎 Create code that can recreate the bean de
fi
nitions
🧠 Let GraalVM do its thing
💾
💾
💾
💾
*
*
There's a little more to it than that
to the code...
Thanks!
1 of 30

More Related Content

More from VMware Tanzu(20)

tanzu_developer_connect.pptxtanzu_developer_connect.pptx
tanzu_developer_connect.pptx
VMware Tanzu153 views
Tanzu Developer Connect - FrenchTanzu Developer Connect - French
Tanzu Developer Connect - French
VMware Tanzu13 views

Recently uploaded(20)

SIG-NOC Tools SurveySIG-NOC Tools Survey
SIG-NOC Tools Survey
CSUC - Consorci de Serveis Universitaris de Catalunya47 views
LLM App Hacking (AVTOKYO2023)LLM App Hacking (AVTOKYO2023)
LLM App Hacking (AVTOKYO2023)
Shota Shinogi212 views
Heatmap for SAP and CIA.pdfHeatmap for SAP and CIA.pdf
Heatmap for SAP and CIA.pdf
AndreeaTom47 views
We aint got no time - Droidcon NairobiWe aint got no time - Droidcon Nairobi
We aint got no time - Droidcon Nairobi
Danny Preussler41 views
Generative AI at the edge.pdfGenerative AI at the edge.pdf
Generative AI at the edge.pdf
Qualcomm Research66 views
MSWMSW
MSW
Wonjun Hwang22 views
Building Stronger BoardsBuilding Stronger Boards
Building Stronger Boards
OnBoard23 views
Orchestration, Automation and Virtualisation Maturity ModelOrchestration, Automation and Virtualisation Maturity Model
Orchestration, Automation and Virtualisation Maturity Model
CSUC - Consorci de Serveis Universitaris de Catalunya54 views

Demystifying Spring Internals