Product Search


What Is Bean Scope In Spring Framework


Java classes or POJO which are managed by Spring Framework are called Bean or Spring Beans and Bean scope in Spring framework or Spring MVC is scope for a bean managed by Spring IOC container. Bean scope refers to the lifecycle and visibility of a bean instance in spring container. The scope determines how long the bean instance will live and how it will be shared with the other parts of application. You may know that Spring is a framework that is based on Dependency Injection and Inversion of Control and provides bean management facilities to Java application. In Spring-managed environment bean (Java Classes) are created and wired by the Spring framework. Spring allows you to define how those beans will be created and the scope of the bean is one of those details. Scope are similar to access modifiers in Java which specifies visibility of a particular class. Singleton and prototype are two common bean scopes which is available on all Spring Application Context while request, session, and global session bean scope are only available on Web aware application Context like WebApplicationContext.


Similarly, Application and web socket scopes are also not standard and they are introduced by the framework which integrates with Spring like Spring WebFlux and Spring Web MVC. WebSocket scope is similar to session scope but it applies to web socket session in a web socket enabled application. On other hand, Application scope ties the lifecycle of a bean with the lifecycle of a servlet context in a Java web application. A single instance of the bean is created in application scope for entire duration of application and its shared between all requests. By the way, if you are new to Spring framework then I also suggest you join a comprehensive and up-to-date course to learn Spring in depth. If you need recommendations, I highly suggest you take a look at Spring Framework 6: Beginner to Guru, one of the comprehensive and hands-on course to learn modern Spring. Now, let's understand both singleton and prototype bean scope in more detail.


Singleton bean scope is default scope for bean declared in Spring and applicable when you don't specify scope attribute while specifying bean details in ApplicationContext.xml or Spring configuration file. Singleton bean scope is like a Singleton pattern in Java where only one instance of the bean is created per Spring container. So no matter how many times you call getBean() method, the same bean instance will be returned if its bean scope is declared as Singleton. While in the case of prototype bean scope, every getBean() call creates a new instance of Spring bean. The difference between Singleton and prototype bean scope is also a popular Spring question. On the other hand request bean scope allows each HTTP request to have its own instance of a bean created and supplied by Spring framework, while session bean scope allows a Web application to have bean instance per session basis. Both of these bean scopes are available on WebApplicationContext or any web-aware application context.


The Last one which is global session bean scope is only applicable to portlet aware bean scope and allows bean instance per global session. In short singleton vs prototype is important which clearly segregates one instance to multiple instances of bean. The newly added Application and WebSocket scope are for Spring WebFlux and Spring Web MVC. Application scope bean is created once and ties with servlet context for entire duration of application and same bean is shared with all request. This means you bean must be thread-safe since its shared with all the request, otherwise it could cause multithreading and concurrency issue. As I said earlier, Web Socket scope is like session scope but its for web socket session and its only application for web socket enabled application. Many programmer doesn't know but Spring also allows developer to create their own bean scope by implementing org.springframework.bean.factory.config.Scope interface. Custom scopes can be really useful in scenarios when standard scope are not enough for example creating a transaction scope which allows bean to live for a transaction, a conversational scope, a cache scope, a tenant scope which can be used in multi-tenant application where bean is scoped with a specific tenant and its not shared between tenants or isolated between tenants.


If you are interested to learn more about Spring basics, you can also check out Spring Framework: Spring Fundamentals course on Pluralsight by Bryan Hansen. It's nice course to learn essential Spring concepts in 2 and half hours. How to specify Bean Scope in Spring Framework? In order to specify bean scope, you can either use Annotation on Spring or you can define it on Application Context, for example in below Spring configuration file AuditService is configured as Singleton using singleton bean scope and PaymentService as prototype bean scope. Since singleton is also default scope in the spring framework, the following declaration is exactly the same and creates bean on singleton scope. Though I prefer explicit declaration to make bean scope loud and clear. Now every time you call getBean("auditService") it will return the same instance of AuditService. In the case of the prototype, beans cope every call to getBean("auditServie") will return different instances of AuditServiceImpl class.



Featured Products






Articles


Lawn Maintenance The Lawncare Man Perth Wa
Advantages of For Sale by Owner Homes
Guidelines For Running Shoes For Girls
What Is Controller
Valuable Web Hosting Tips
Marketing Tips For Clickbank Affiliates
Online Furniture Checklist To Buy
Why Espresso Coffee Machines Are The Best
Centarticles Review Content Article Is Important
Believe It Or Not Anti Snoring Tools You Never Thought Would Work
Mens Motorcycle Jackets Are Designed To Complement The Overall Design And Look Of Your Bike
Know About The Benefits Of Learning Chess Online
Supple Drink Reviews
Car Buying Apps For Iphone And Android
Learn More About Landscape Paintings
Best Lawn Mowers
How To Improve Your Golf Swing
How To Select The Best Height Adjustable Desk
Useful Tips For Buying The Perfect Kitchen Appliances For Your Home
Monday ? ?s College Roundup St Joseph ? ?s Sweeps Baseball Doubleheader Against Elms College Portland Press Herald
Brigade Calista New Launch In Bangalore
How To Host A Bridgertoninspired Afternoon Tea Tasty Recipes - Video
Brad At Cd Baby August 28
Workers Compensation Attorney For Work Injury Cases
Grow Your Knowledge About U S Soccer Development Academy
Get to Know About US Immigration Form Application For Travel Document!
Hot Tools Professional Curling Iron Review
Know The Most About Commercial Tables For Business Usage
Dyson Ariwrap Dupes
The Astonishing Secret To Make Money Online Fast
Living In Largo Dental Implants Are Readily Available
What Are The Benefits Of A Cuisinart Toaster
Vera Wang Perfume A Fast Growing Player In The Fragrance Industry
Getting The Best Possible Golf Course Supplies
San Francisco S Bay Bridge Keeps Its Computer Controlled Lights
House Renovation Software
Build Backyard Sheds Has Plans To Build Tool Shed
A Quick Guide To Selecting A New Toaster Oven
Find The Best Wedding Dresses Look Beautiful On Your Wedding Day
Methods To Shop For Ladies Lingerie
Jam Sports Marketing Gives No Cost Items To Schools
Asp Net Development Most Popular Use Cases
Camping Tents Your Choice Your Comfort
Tips For Maintaining Your Motorcycle Tires
Old Macdonald Had A Farm Kids Nursery Rhymes - Video
Cosmetic Packaging Its Significance In Marketing Your Product
The Importance Of Car Servicing The Right Way
Guide To Buying A Used Snowboard
Need Guidance With A Michigan Bar Exam Appeal
Check Out The Options When You Jailbreak Iphone