简体   繁体   English

通过接口公开单例类的正确方法

[英]proper way of exposing singleton class through interface

I have a singleton class that has to be exposed as a service to other applications using interface. 我有一个单例类,必须使用接口将它作为服务公开给其他应用程序。

for example: 例如:

public class MySingleSingletonClass{

    private static final MySingleSingletonClass THIS_INSTANCE = new MySingleSingletonClass();

    private MySingleSingletonClass() {}

    public static MySingleSingletonClass getInstance(){
       return THIS_INSTANCE;
    }

    //do other staff
   public int methodA(){
     //some service
   }

}

Now if I want to expose all the services of this class through interface, here is my first attempt : 现在,如果我想通过接口公开此类的所有服务,这是我的第一次尝试:

public interface MyServiceInterface{
    int methodA();
    MyServiceInterface getInstanceThroughService(); 
}

and when MySingleSingletonClass implements this interface : 当MySingleSingletonClass实现此接口时:

public class MySingleSingletonClass implements MyServiceInterface{

    private static final MySingleSingletonClass THIS_INSTANCE = new MySingleSingletonClass();

    private MySingleSingletonClass() {}

    public static MySingleSingletonClass getInstance(){
       return THIS_INSTANCE;
    }

   @Override
   public int methodA(){
     //some service
   }

   @Override
   MyServiceInterface getInstanceThroughService(){
     return MySingleSingletonClass.getInstance();
   }

}

I see two problems with this kind of implementation, 我看到这种实施有两个问题,

first if I use a framework like spring, and I try to get a bean of type MyServiceInterface how will the class be instantiated? 首先,如果我使用像spring这样的框架,并且尝试获取MyServiceInterface类型的bean,那么如何实例化该类? I read that spring will still call the private contractor of the class using reflection. 我读到Spring仍会使用反射来调用该类的私有承包商。 will this instance still be singleton ? 这个实例还会是单例吗?

Second , if Spring gives me the instance already, I don't see the point of calling getInstanceThroughService() method using the instance itself. 其次 ,如果Spring已经为我提供了实例,则我看不到使用实例本身调用getInstanceThroughService()方法的意义。 feels like there is a problem with this implementation. 感觉此实现存在问题。

thank you 谢谢

Typical spring beans are singletons by default! 默认情况下,典型的春豆是单例!

@Service
public class MySingleSingletonClass{

   public int methodA(){...}
}

@Service
public class ConsumerA{
   @Autowired
   private MySingleSingletonClass mssc;
}

@Service
public class ConsumerB{
   @Autowired
   private MySingleSingletonClass mssc;
}

In this scenario you will have 3 beans: - ONE MySingleSingletonClass - one ConsumerA - one ConsumerB 在这种情况下,您将拥有3个bean:-一个MySingleSingletonClass-一个ConsumerA-一个ConsumerB

The reference mssc in ConsumerA and ConsumerB will point to the SAME instance of MySingleSingletonClass . ConsumerAConsumerB的引用mssc将指向MySingleSingletonClass的SAME实例。

So you do not need the have to implement the standart Singleton pattern, Spring will do it for you. 因此,您无需实现标准的Singleton模式,Spring会为您完成。

You can use spring's factory-method http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html to tell spring to use getInstanceThroughService() to get new instances which in turn you will give same cached instance and hence maintaining your singleton. 您可以使用spring的工厂方法http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html告诉Spring使用getInstanceThroughService()来获取新实例,而您又可以使用将提供相同的缓存实例,从而保持您的单例。

But, if you are using spring then you dont need to maintain singleton object yourself. 但是,如果您使用的是Spring,则无需自己维护单例对象。 Spring does it automatically (unless of-course your class is being used outside of spring container in same jvm/classloader). Spring会自动执行此操作(当然,除非在同一jvm / classloader中在Spring容器外部使用您的类,否则当然)。

Spring is a framework that implements a design pattern called inversion of control (or IoC). Spring是一个框架,可实现称为控制反转 (或IoC)的设计模式。 One application of IoC is to allow an external framework to configure and instantiate parts of the application, indeed, this is the function of spring beans. IoC的一个应用程序是允许外部框架配置和实例化应用程序的各个部分,的确,这是Spring bean的功能。 Spring beans allow you to configure application resources using a configuration file and access those resources in your application. Spring Bean允许您使用配置文件配置应用程序资源,并在应用程序中访问这些资源。

Singletons (and prototype beans) are managed by spring, so there is no need to implement the Singleton design pattern yourself. Singleton(和原型bean)由Spring管理,因此不需要自己实现Singleton设计模式。 You only need to create a Spring-compliant bean and declare it in the Spring configuration. 您只需要创建一个Spring兼容的bean并在Spring配置中声明它即可。 You can then access it from the application context. 然后,您可以从应用程序上下文访问它。

Your bean can still implement an interface as it normally would, and you can use the interface in your application. 您的bean仍然可以像往常一样实现接口,并且可以在应用程序中使用该接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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