Bean Scopes - Overview

Chad Darby
A free video tutorial from Chad Darby
Popular Java Spring Instructor - Best Seller
4.6 instructor rating • 8 courses • 390,787 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:47:36 of on-demand video • Updated October 2020

  • 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 175,000+ students that are already enrolled! Over 44,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 Tutor: In this video, we're gonna discuss the bean scopes available in Spring. So what exactly are scopes? Well, a scope refers to the lifecycle of a bean, for example, it tells you how long the bean will live, how many instances will be created and also how is the bean shared in the Spring environment? So the default scope for a bean is singleton, so here's an example here of just some bean code, that we had earlier and we didn't explicitly give a scope, so by default, the scope is singleton. But now you're probably wondering, well, what exactly is a singleton? (laughs) we know it's a default scope, but what is it? Well, for a singleton, the Spring Container creates only one instance of the bean, it's cached in memory and then all requests for that bean will return a shared reference to the same bean, so the end result is that there is only one bean and everyone will share it. Here's a nice diagram to kind of show you this example. In this example, we have theCoach equals context.getBean myCoach and it'll give you a reference to like a TrackCoach, that you have defined and then later on in the code, if you would also do a similar thing, saying context.getBean myCoach, the same bean id, then it'll basically give you a reference to the same bean. We have these two object references here and they point to the same area of memory or they point to the same bean, so again, Spring makes use of a singleton, it'll create only one bean and then share it for everyone who requests that bean, so the singleton scope is default and the best use case for this is for a stateless bean, where you don't need to maintain any state. You can explicitly specify the bean scope, I mean, by now you know that by default, you have the singleton scope, but if you want to explicitly specify, then you make use of the scope attribute, so you say scope equals singleton and that'll make it a singleton bean and that's kind of the preferred approach to minimize the number of beans, that are created. But now, there are additional Spring bean scopes, that you can make use of. We've already covered singleton, there's also the prototype scope, which creates a new bean instance for each container request and we'll see examples of that coming up and we'll also use that as a demo in the video coming up, then the next three items here, request, session and global-session, these scopes are only used in a web environment, so request is for a given web request, session is for a HTTP web session, for like session tracking for like maybe a shopping cart or something and then global-session is scope application-wide, but we'll talk more about these later, when we get into the Spring MVC section, but for now, we'll simply focus on singleton and prototype. So here's an example of using prototype scope, so again, remember, prototype scope, a new object is created for each request, so in this example here, I have my bean id of myCoach and I have scope equals prototype, so that means that every time I make a request for this myCoach, they'll create a new instance each time. So, a nice little diagram here. So the line of code at the top, theCoach equals context.getBean myCoach, it'll create a new instance of that bean, I'll get a reference to it and then a similar thing here, when I say alphaCoach equals context.getBean myCoach, it'll create a new object for you and you'll have your own reference. So the prototype scope is good for keeping track of stateful data, so again, whenever you see prototype, just think of the new keyword, it's gonna create a new bean for each request for that component or that object. Alright, so this is some really good stuff. Let's go ahead and move forward, in the next videos, we're gonna dive into Eclipse and we'll actually write some code, that'll make use of the singleton scope and the prototype scope, so you can see everything in action. Alrighty, I'll see you in the next video.