Spring in action learns note nine: How to prove that the object is created differently each time the scope is prototype.

Source: Internet
Author: User

There are four values for scope in spring: Singleton, prototype, session, request. The session and request are in the Web application.

The following shows that the objects created each time the scope is prototype are different.

The sample code is as follows:

1  Packagecom.advancedWiring.ambiguityIniAutowiring2;2 3 ImportOrg.springframework.beans.factory.BeanNameAware;4 Importorg.springframework.beans.factory.config.ConfigurableBeanFactory;5 ImportOrg.springframework.context.annotation.Scope;6 Importorg.springframework.stereotype.Component;7 8 /**9 * Created by ${Quinlinson} on 2017/6/9.Ten  */ One @Component A@Scope (Configurablebeanfactory.scope_prototype) -  Public classStudentImplementsbeannameaware{ -     PrivateString name; the     PrivateInteger age; -  -      PublicString GetName () { -         returnname; +     } -  +      Public voidsetName (String name) { A          This. Name =name; at     } -  -      PublicInteger getage () { -         returnAge ; -     } -  in      Public voidsetage (Integer age) { -          This. Age =Age ; to     } +  - @Override the      Public voidsetbeanname (String name) { *System.out.println ("The Student Bean name is:" +name); $     }Panax Notoginseng}
1  Packagecom.advancedWiring.ambiguityIniAutowiring2;2 3 Importorg.springframework.beans.factory.config.ConfigurableBeanFactory;4 ImportOrg.springframework.context.annotation.Bean;5 ImportOrg.springframework.context.annotation.ComponentScan;6 Importorg.springframework.context.annotation.Configuration;7 ImportOrg.springframework.context.annotation.Scope;8 9 /**Ten * Created by ${Quinlinson} on 2017/6/9. One  */ A @Configuration - @ComponentScan -  Public classStudentconfig { the  -}
1  Packagecom.advancedWiring.ambiguityIniAutowiring2;2 3 ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7 * Created by ${Quinlinson} on 2017/6/9.8  */9  Public classTest {Ten      Public Static voidMain (string[] args) { One      AAnnotationconfigapplicationcontext AC =NewAnnotationconfigapplicationcontext (Studentconfig.class); -Student Student = Ac.getbean (Student.class); -Student Student2 = Ac.getbean (Student.class); theSystem.out.println (student==student2); -     } -}/**output:
false/

The result of the test class above is false, which shows that each bean created by spring is a completely new object when scope is prototype.

Spring in action learns note nine: How to prove that the object is created differently each time the scope is 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.