In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Springbean scope attribute what is the difference between singleton and prototype, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
1.singleton
When the scope of a bean is set to singleton, only one shared bean instance exists in the Spring IOC container, and all requests for bean will only return the same instance of bean as long as the id matches the bean definition.
In other words, when a bean definition is set to singleton scope, the Spring IOC container only creates a unique instance of that bean definition. The singleton instance will be stored in the singleton cache, and all subsequent requests and references to the bean will return the cached object instance. It is important to note that the singleton scope is completely different from the singleton in the GOF design pattern, which indicates that only one class exists in a ClassLoader, while the singleton here indicates that a container corresponds to a bean, that is, when a bean is identified as singleton. Only one of this bean will exist in the IOC container of spring.
ApplicationContextER.xml:
Test the code:
Public class GetDate {public static void main (String [] args) {/ / get application context interface ApplicationContext apl = new ClassPathXmlApplicationContext ("applicationContextER.xml"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); try {/ / repeatedly call getBean to check the time Date date = (Date) apl.getBean ("get_date"); / / take a 3-second break Thread.sleep (1000) System.out.println ("-:" + simpleDateFormat.format (date)); Date date1 = (Date) apl.getBean ("get_date"); Thread.sleep (1000); System.out.println ("-:" + simpleDateFormat.format (date1)); Date date2 = (Date) apl.getBean ("get_date"); Thread.sleep (1000) System.out.println ("-:" + simpleDateFormat.format (date2)); System.out.println ("date is date1:" + (date = = date1)); System.out.println ("date1 is date2:" + (date1 = = date2));} catch (Exception e) {}}
Test results:
23 get_date'23:05:04.298 04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Creating shared instance of singleton bean 'get_date'23:05:04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Creating instance of bean' get_date'23:05:04.308 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Eagerly caching bean 'get_date' to allow for resolving potential circular references23:05:04.309 [main] DEBUG org.springframework.beans. Factory.support.DefaultListableBeanFactory-Finished creating instance of bean 'get_date'23:05:04.310 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext-Unable to locate LifecycleProcessor with name' lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3108bc] 23 Fringe 05V 04.310 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Returning cached instance of singleton bean 'lifecycleProcessor'23:05:04.311 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver-Could not find key' Spring.liveBeansView.mbeanDomain' in any property source23:05:04.316 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Returning cached instance of singleton bean 'get_date'-:2019-12-21 23 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory 05 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory 0423 purl 055.320 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Returning cached instance of singleton bean' get_date'-:2019-12- 21 23 23:05:04date is date1 0515 23:05:04date is date1: truedate1 is date2: true [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Returning cached instance of singleton bean 'get_date'-:2019-12-21
As you can see from the above results, after the object is created, it is stored in the cache. The objects that are acquired each time are fetched from the cache, not newly created. So the object is the same every time.
2.prototype
For prototype-scoped bean, each request (injecting it into another bean or programmatically calling the container's getBean () method) produces a new bean instance, which is equivalent to an new operation. For prototype-scoped bean, it is very important that Spring is not responsible for the entire life cycle of a prototype bean, after the container initializes, configures, decorates, or assembles a prototype instance. Give it to the client and then turn a blind eye to the prototype instance.
Regardless of the scope, the container will call the initialization lifecycle callback methods of all objects, while for prototype, any configured destructor lifecycle callback methods will not be called. It is the responsibility of the client code to clear prototype-scoped objects and release any expensive resources held by prototype bean. One possible way for the Spring container to release resources occupied by the singleton-scoped bean is to use the post processor of bean, which holds a reference to the bean to be purged.
ApplicationContextER.xml:
Test results:
23PUR 01RV 51.314 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Creating instance of bean 'get_date'23:01:51.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Finished creating instance of bean' get_date'-:2019-12-21 23Rd 01RV 5123RV 01RV 52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Creating instance of bean 'get_date 52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Finished creating instance of bean 'get_date'-:2019-12-21 23 Finished creating instance of bean 5223 Finished creating instance of bean 53.330 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Creating instance of bean' get_date'23:01:53.331 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory-Finished creating instance of bean 'get _ date'-:2019-12-21 23:01:53date is date1: falsedate1 is date2: false
As you can see from the above results, a new object is created each time, so the object is different each time.
Summary: as can be seen from 1 and 2, you can use singleton when you need a global unique label, and singleton only creates one object, which consumes less resources. But using singleton may have thread-safe problems, and you need to use prototype at this time. To consider concurrency, it is recommended to use prototype.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.