简体   繁体   中英

Using Java Singleton class in Spring as prototype

I have a class which is plain Java Singleton.

public class MySingleton {

    Private static instance  = new MySingleton();

    private MySingleton(){}

    public static getInstance(){
       return instance;
    }

Now if I use this singleton class as a bean in Spring in prototype scope will Spring create multiple instance of this pure singleton class?

<bean id="supposedToBeSingleton" class="MySingleton" scope="prototype" />

您对 spring 的定义不正确,这就是它带来错误的原因,您应该将其定义为其中 YourClassName 代表您的班级(父级或子级)

Logically Spring will not use

         public static getInstance(){
           return instance;
         }

which is returning the singleton so it will return prototype using the default constructor. But to be sure you can just run the test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM