Bean Lifecycle - Overview

Chad Darby
A free video tutorial from Chad Darby
Popular Java Spring Instructor - Best Seller
4.6 instructor rating • 8 courses • 426,724 students

Learn more from the full course

Spring & Hibernate for Beginners (includes Spring Boot)

Spring 5: Learn Spring 5 Core, AOP, Spring MVC, Spring Security, Spring REST, Spring Boot 2, Thymeleaf, JPA & Hibernate

40:54:10 of on-demand video • Updated April 2021

  • Develop a REAL-TIME project with Spring MVC, Spring REST, Spring Boot and Hibernate CRUD ... all from SCRATCH
  • You will TYPE IN EVERY LINE of code with me in the videos. I EXPLAIN every line of code to help you learn!
  • LEARN key Spring 5 features: Core, Annotations, Java Config, AOP, Spring MVC, Hibernate and Maven
  • I am a RESPONSIVE INSTRUCTOR ... post your questions and I will RESPOND in 24 hours.
  • POPULAR VIDEOS for: Spring Boot 2, Spring Security, Spring REST, Spring Data JPA, Spring Data REST and Thymeleaf
  • Join an ACTIVE COMMUNITY of 185,000+ students that are already enrolled! Over 47,000+ Reviews - 5 STARS
  • Students have LANDED NEW JOBS with the skills from this course. Spring and Hibernate developers are in HIGH-DEMAND!
  • You can DOWNLOAD all videos, source code and PDFs. Perfect for offline LEARNING and REVIEW.
English Instructor: In this video, we're gonna discuss bean lifecycle methods. So when the Spring container first starts there are a couple of things that happens. First off, the beans are instantiated, and then the actual dependencies are injected, next you have some internal Spring processing that occurs with the bean factory, and then you have the option of adding your own custom initialization code, and then at that point the bean is ready for use. So you can call methods on it, do work with the bean, so on and so forth. At a certain point the containers actually shutdown meaning your application is shutdown like what context.close, then you also have a chance to call your custom destroy method, and that code'll execute before the actual application is stopped or before the actual beans lifecycle is over. The one thing you want to take a look at are those two orange sections here adding your own custom methods. What you can do is you can add your own custom code that happens during bean initialization. So you can call custom business logic, you can set up handles, so like databases, or sockets, or whatever. You can also do a similar thing when a bean is actually being destroyed or destructed. So again, you can call any custom business methods, or you can clean up any handles that you may have to resources like databases, sockets, or files. So basically what this provides here is that during the bean lifecycle, Spring allows you to call some of your custom code, and these are what we call hooks, where you can actually hook in codes to execute during bean initialization or bean destruction. All right, so now, how would you do this? Well, you simply make configuration entry in your XML file. So for bean initialization, you make use of this attribute called init-method, and then you give the actual method name that you would like for Spring to call on your bean. Now this method name can be any method name. Here I called doMyStartupStuff. It could be called fubar or whatever, and we can also do a similar thing for the destroy method. So again, you simply make a configuration entry here in your XML file. You simply give the method name you want Spring to call for destroy, and again, it can be any name. All right, so this all looks kinda good. What's the basic development process? So again, I love my step-by-step. So the first thing you do is you simply define your methods for the init and destroy in your bean class, then the next step is you simply configure those method names in the Spring configuration file, and in the next video, we'll dive into Eclipse, and we'll actually walk through this development process, and we'll test out this feature. So I'll see you in the next video.