-
Notifications
You must be signed in to change notification settings - Fork 0
/
spring-bean-lifecycle.html
18 lines (17 loc) · 18.4 KB
/
spring-bean-lifecycle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html><html lang="de-ch"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Spring Bean Lifecycle - Finecloud</title><meta name="description" content="Part 1 Of course, instantiating the class will come first. The class will populate properties after it has been created. It will therefore set up any properties you may have. Then, if they exist, we do have some interfaces that we can implement. These methods…"><meta name="generator" content="Publii Open-Source CMS for Static Site"><link rel="stylesheet" href="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism-black.css"><link rel="canonical" href="https://www.finecloud.ch/spring-bean-lifecycle.html"><link rel="alternate" type="application/atom+xml" href="https://www.finecloud.ch/feed.xml"><link rel="alternate" type="application/json" href="https://www.finecloud.ch/feed.json"><meta property="og:title" content="Spring Bean Lifecycle"><meta property="og:site_name" content="Finecloud"><meta property="og:description" content="Part 1 Of course, instantiating the class will come first. The class will populate properties after it has been created. It will therefore set up any properties you may have. Then, if they exist, we do have some interfaces that we can implement. These methods…"><meta property="og:url" content="https://www.finecloud.ch/spring-bean-lifecycle.html"><meta property="og:type" content="article"><link rel="shortcut icon" href="https://www.finecloud.ch/media/website/finecloud.png" type="image/png"><link rel="stylesheet" href="https://www.finecloud.ch/assets/css/style.css?v=39da73365516a098a9b73b721fc970e2"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.finecloud.ch/spring-bean-lifecycle.html"},"headline":"Spring Bean Lifecycle","datePublished":"2023-04-04T15:29","dateModified":"2023-04-28T22:33","description":"Part 1 Of course, instantiating the class will come first. The class will populate properties after it has been created. It will therefore set up any properties you may have. Then, if they exist, we do have some interfaces that we can implement. These methods…","author":{"@type":"Person","name":"Finecloud","url":"https://www.finecloud.ch/authors/finecloud/"},"publisher":{"@type":"Organization","name":"Finecloud"}}</script><meta name="google-site-verification" content="seFY9U12uiEq5U3_MyZiX6XWzk0AVFl9zITr2ZKsytY"></head><body><div class="site-container"><header class="top" id="js-header"><a class="logo" href="https://www.finecloud.ch/">Finecloud</a><nav class="navbar js-navbar"><button class="navbar__toggle js-toggle" aria-label="Menu" aria-haspopup="true" aria-expanded="false"><span class="navbar__toggle-box"><span class="navbar__toggle-inner">Menu</span></span></button><ul class="navbar__menu"><li><a href="https://www.finecloud.ch/" target="_self">Blog</a></li><li><a href="https://www.finecloud.ch/tags/" target="_self">Tags</a></li></ul></nav><div class="search"><div class="search__overlay js-search-overlay"><div class="search__overlay-inner"><form action="https://www.finecloud.ch/search.html" class="search__form"><input class="search__input js-search-input" type="search" name="q" placeholder="search..." aria-label="search..." autofocus="autofocus"></form><button class="search__close js-search-close" aria-label="Close">Close</button></div></div><button class="search__btn js-search-btn" aria-label="Search"><svg role="presentation" focusable="false"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#search"/></svg></button></div></header><main><article class="post"><div class="hero"><figure class="hero__image hero__image--overlay"><img src="https://www.finecloud.ch/media/website/download.jpg" srcset="https://www.finecloud.ch/media/website/responsive/download-xs.jpg 300w, https://www.finecloud.ch/media/website/responsive/download-sm.jpg 480w, https://www.finecloud.ch/media/website/responsive/download-md.jpg 768w, https://www.finecloud.ch/media/website/responsive/download-lg.jpg 1024w, https://www.finecloud.ch/media/website/responsive/download-xl.jpg 1360w, https://www.finecloud.ch/media/website/responsive/download-2xl.jpg 1600w" sizes="100vw" loading="eager" alt=""></figure><header class="hero__content"><div class="wrapper"><div class="post__meta"><time datetime="2023-04-04T15:29">April 4, 2023</time></div><h1>Spring Bean Lifecycle</h1></div></header></div><div class="wrapper post__entry"><div class="post__toc"><h3>Table of Contents</h3><ul><li><a href="#mcetoc_1gt66f14j8k">Part 1</a></li><li><a href="#mcetoc_1gt66f14j8l">Part 2</a></li><li><a href="#mcetoc_1gt66f14j8m">Callback Interfaces</a></li><li><a href="#mcetoc_1gt66f14j8n">Lifecycle Annotations</a></li><li><a href="#mcetoc_1gt66f14j8o">Bean Post Processors</a></li><li><a href="#mcetoc_1gt66f14j8p">Aware Interfaces</a></li></ul></div><figure class="post__image"><img loading="lazy" src="https://www.finecloud.ch/media/posts/83/lifecycle.png" alt="" width="1303" height="562" sizes="100vw" srcset="https://www.finecloud.ch/media/posts/83/responsive/lifecycle-xs.png 300w, https://www.finecloud.ch/media/posts/83/responsive/lifecycle-sm.png 480w, https://www.finecloud.ch/media/posts/83/responsive/lifecycle-md.png 768w, https://www.finecloud.ch/media/posts/83/responsive/lifecycle-lg.png 1024w, https://www.finecloud.ch/media/posts/83/responsive/lifecycle-xl.png 1360w, https://www.finecloud.ch/media/posts/83/responsive/lifecycle-2xl.png 1600w"></figure><h2 id="mcetoc_1gt66f14j8k">Part 1</h2><div id="ct-sidebar-scroll-container" class="sidebar--content---4z0-" data-purpose="sidebar-content"><div class="transcript--transcript-panel--kfMxM" dir="auto" data-purpose="transcript-panel"><div class="transcript--cue-container--wu3UY"><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Of course, instantiating the class will come first. The class will populate properties after it has been created. It will therefore set up any properties you may have. Then, if they exist, we do have some interfaces that we can implement. These methods will be executed by Spring if you've implemented these interfaces. As you can see, BeanNameAware's setBeanName method is called. Following that are BeanFactoryAware, ApplicationContextAware, and PreInitialization, which will be carried out using a post bean processor. As a result, we can hook into these callbacks before moving on to the initializing beans' afterPropertiesSetet. Additionally, we can offer special initialization techniques. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);" data-purpose="cue-text">And then one of the last things is the post initialization. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">So these are all steps that the bean goes through before it is ready to use.</span></div><div class="transcript--cue-container--wu3UY"><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue"><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Before the application is deemed ready for use, Spring will go through and carry out all these actions on each bean in the context.</span></p><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue"><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">This provides you with a variety of connections. You do have a great deal of flexibility and control over this. Sincerely speaking, you hardly ever need to hook into this. There are use cases that you encounter, but it's rare that you require this much control over the production of the beans. You're typically doing something a little complicated when you do it. But I believe that awareness is the most important lesson to learn. Use this when you need it; it is there and available for you. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">So here this is just the bean becoming ready for use.</span></p></div><div class="transcript--cue-container--wu3UY"><h2 id="mcetoc_1gt66f14j8l" tabindex="-1" role="button" data-purpose="transcript-cue">Part 2</h2><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue"><span data-purpose="cue-text">There is also a lifecycle for the beans to be terminated. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">So when the container is being shut down, the application receives some type of shutdown event. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">There is a lifecycle for the shutdown. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">So you can annotate a bean method with the annotation PreDestroy that will get annotated. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">And then you can also implement a disposable bean interface that provides a destroy method so that the </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">annotation will come first and then the destroy method. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">And at that point we call the bean terminated.</span></p><h2 id="mcetoc_1gt66f14j8m" tabindex="-1" role="button" data-purpose="transcript-cue">Callback Interfaces</h2></div><div class="transcript--cue-container--wu3UY"><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue">Consequently, we did discuss a few of the interfaces. There are two interfaces in Spring that you can use to implement callback events. You have the afterPropertiesSet from the InitializingBean. So if you implement that interface, that method will be called. Finally, there is the DisposalBean. We discussed the fact that that is one of the last functions called before the bean is terminated. </p><h2 id="mcetoc_1gt66f14j8n" tabindex="-1" role="button" data-purpose="transcript-cue">Lifecycle Annotations</h2></div><div class="transcript--cue-container--wu3UY"><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue"><span data-purpose="cue-text">You do also have a couple annotations, and these were in that flowchart as well. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">We have <strong>PostConstruct</strong>, so the bean has been constructed, but it has not been returned to the requesting </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">object. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">And then we also have <strong>PreDestroy</strong>. </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">So if you remember in the termination workflow, this was called just before the interface method is </span><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">called.</span></p><h2 id="mcetoc_1gt66f14j8o" tabindex="-1" role="button" data-purpose="transcript-cue">Bean Post Processors</h2><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue">These can be useful if you're working with a third-party object of some kind that you need to create, put into your context, and update in some way. like configuring a component that is typically not managed by Spring. So, you now have some capabilities. The main point you should take away from this post is that bean processors are referred to as such internationally. In essence, you receive this event, and it will be examined. If you implement it, this will be called 100 times in the case where you have 100 beans and every object or bean. In order to implement this typically, you're probably searching for a particular bean, a kind of bean that </p></div><div class="transcript--cue-container--wu3UY"><h2 id="mcetoc_1gt66f14j8p" tabindex="-1" role="button" data-purpose="transcript-cue">Aware Interfaces</h2><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue">Finally, Spring has over 14 aware interfaces as well. Therefore, these extend interface are also known as aware. Thus, they are a continuation of that. They number fourteen. The Spring framework largely makes use of these. Therefore, if you're creating Spring applications, you should be aware that you use these. Therefore, if you have a use case, you may encounter situations where you must use these. These might be very useful.</p></div><div class="transcript--cue-container--wu3UY"><p class="transcript--underline-cue--3osdw" tabindex="-1" role="button" data-purpose="transcript-cue"><span data-purpose="cue-text">These are some of the current ones:</span></p></div><div class="transcript--cue-container--wu3UY"><table style="border-collapse: collapse; width: 100%;" border="1"><tbody><tr><td style="width: 49.9288%;"><strong>Aware Interface</strong></td><td style="width: 49.9288%;"><strong>Description</strong></td></tr><tr><td style="width: 49.9288%;">ApplicationContextAware</td><td style="width: 49.9288%;">Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in.</td></tr><tr><td style="width: 49.9288%;">ApplicationEventPublisherAware</td><td style="width: 49.9288%;">Set the ApplicationEventPublisher that this object runs in.</td></tr><tr><td style="width: 49.9288%;">BeanClassLoaderAware</td><td style="width: 49.9288%;">Callback that supplies the bean class loader to a bean instance.</td></tr><tr><td style="width: 49.9288%;">BeanFactoryAware</td><td style="width: 49.9288%;">Callback that supplies the owning factory to a bean instance.</td></tr><tr><td style="width: 49.9288%;">BeanNameAware</td><td style="width: 49.9288%;">Set the name of the bean in the bean factory that created this bean.</td></tr><tr><td style="width: 49.9288%;">BootstrapContextAware</td><td style="width: 49.9288%;">Set the BootstrapContext that this object runs in.</td></tr></tbody></table></div></div></div></div><footer class="wrapper post__footer"><p class="post__last-updated">This article was updated on April 28, 2023</p><ul class="post__tag"><li><a href="https://www.finecloud.ch/tags/java/">java</a></li><li><a href="https://www.finecloud.ch/tags/softwareentwicklung/">software development</a></li><li><a href="https://www.finecloud.ch/tags/spring/">spring</a></li><li><a href="https://www.finecloud.ch/tags/spring-framework/">spring-framework</a></li></ul><div class="post__share"></div></footer></article><nav class="post__nav"><div class="post__nav-inner"><div class="post__nav-prev"><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-prev"/></svg> <a href="https://www.finecloud.ch/spring-annotations.html" class="post__nav-link" rel="prev"><span>Previous</span> Spring Annotations</a></div><div class="post__nav-next"><a href="https://www.finecloud.ch/project-lombok.html" class="post__nav-link" rel="next"><span>Next</span> Project Lombok </a><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-next"/></svg></div></div></nav><div class="post__related related"><div class="wrapper"><h2 class="h5 related__title">You should also read:</h2><article class="related__item"><div class="feed__meta"><time datetime="2023-05-03T20:46" class="feed__date">Mai 3, 2023</time></div><h3 class="h1"><a href="https://www.finecloud.ch/spring-boot-on-kubernetes.html">Spring Boot on Kubernetes</a></h3></article><article class="related__item"><div class="feed__meta"><time datetime="2023-05-02T21:45" class="feed__date">Mai 2, 2023</time></div><h3 class="h1"><a href="https://www.finecloud.ch/building-spring-boot-docker-images.html">Building Spring Boot Docker Images</a></h3></article><article class="related__item"><div class="feed__meta"><time datetime="2023-04-10T08:36" class="feed__date">April 10, 2023</time></div><h3 class="h1"><a href="https://www.finecloud.ch/spring-exception-handling.html">Spring Exception Handling</a></h3></article></div></div></main><footer class="footer"><div class="footer__copyright"><p>Powered by Publii</p></div><button onclick="backToTopFunction()" id="backToTop" class="footer__bttop" aria-label="Back to top" title="Back to top"><svg><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#toparrow"/></svg></button></footer></div><script>window.publiiThemeMenuConfig = {
mobileMenuMode: 'sidebar',
animationSpeed: 300,
submenuWidth: 'auto',
doubleClickTime: 500,
mobileMenuExpandableSubmenus: true,
relatedContainerForOverlayMenuSelector: '.top',
};</script><script defer="defer" src="https://www.finecloud.ch/assets/js/scripts.min.js?v=6ca8b60e6534a3888de1205e82df8528"></script><script>var images = document.querySelectorAll('img[loading]');
for (var i = 0; i < images.length; i++) {
if (images[i].complete) {
images[i].classList.add('is-loaded');
} else {
images[i].addEventListener('load', function () {
this.classList.add('is-loaded');
}, false);
}
}</script><script defer="defer" src="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism.js"></script></body></html>