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.