Discrimination Singleton and prototype

Source: Internet
Author: User

<BeanID= "Person1"class= "Com.bean.life.Person">    < Propertyname= "Name">        <value>Xiao ming</value>    </ Property></Bean><BeanID= "Person2"class= "Com.bean.life.Person">    < Propertyname= "Name">        <value>Little Red</value>    </ Property></Bean>
Just two days of contact with spring, has been a misunderstanding of these two concepts, always thought that Singleton said that all implementations of "class with the same name" Bean point to the same Java instance, the above bean as an example, previously thought Person1, Person2 is the same Com.bean.life.Person object reference, so foolish, whether the scope of the bean is singleton or prototype, Person1, Person2 are different instances!
The positive solution is that each bean configured in the XML is a different instance!
  The singleton we're talking about is when we get a bean with getbean () or ref, whether we get the same bean or create a new bean instance every time. In spring, the Singleton and prototype are all aimed at a "one" bean. So:SingletonRefers to a single bean, each bean has only one instance object, no matter how many times the Getbean () method is called, no matter how many ref dependencies, there is only one instance of the bean in the spring container.prototypeRefers to the bean, each time the Getbean () method is called, or if there is a ref dependency, a completely new instance of the bean is created, with multiple bean instances in the container. Note: The default scope in Spring is: Singleton.

Discrimination Singleton and prototype

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.