java Singleton - 通过反射防止多重创建
我有一个这样的单身人士。
public class BookingFactory {
private final static BookingFactory instance;
static {
instance = new BookingFactory();
}
public static BookingFactory getInstance() {
return instance;
}
private BookingFactory() {
System.out.println("Object is created.");
}
}
public class Test {
BookingFactory instance = BookingFactory.getInstance();
instance = BookingFactory.getInstance();
Class<?> clazz = Class.forName("com.test.BookingFactory");
Constructor pvtConstructor = clazz.getDeclaredConstructors()[0];
// Set its access control
pvtConstructor.setAccessible(true);
// Invoke Private Constructor
BookingFactory notSingleton = (BookingFactory) pvtConstructor.newInstance(null);
}
当我运行这个程序时,我看到不止一条打印输出消息。有什么方法可以防止此单例从此反射中多次实例化吗?
谢谢。
I have a singleton like this.
public class BookingFactory {
private final static BookingFactory instance;
static {
instance = new BookingFactory();
}
public static BookingFactory getInstance() {
return instance;
}
private BookingFactory() {
System.out.println("Object is created.");
}
}
public class Test {
BookingFactory instance = BookingFactory.getInstance();
instance = BookingFactory.getInstance();
Class<?> clazz = Class.forName("com.test.BookingFactory");
Constructor pvtConstructor = clazz.getDeclaredConstructors()[0];
// Set its access control
pvtConstructor.setAccessible(true);
// Invoke Private Constructor
BookingFactory notSingleton = (BookingFactory) pvtConstructor.newInstance(null);
}
When I run this, I saw more than one printout message. Is there any way to prevent this singleton from being instantiated more than once from this reflection?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用
枚举
。枚举是好的单例。您无法通过反射创建枚举。
getInstance() 方法是多余的,但可以更轻松地运行测试,并抛出以下异常:
哦,看,有人已经给出了枚举答案。无论如何发布以获得更多完整性。
Try using an
enum
. enums make for good Singletons.You can't create an enum via reflection.
The getInstance() method is superfluous but makes it easier to run your test, throwing the following exception:
Oh look, someone already gave the enum answer. Posting anyway for more completeness.
在构造函数中进行断言:
Make the assertion in the constructor:
改编自使用延迟加载时进行Java单例反射证明:
构造函数有一个检查以查看如果调用者可以访问它。正如链接所解释的,需要创建一个允许 Singleton 类本身调用构造函数的策略文件。
Bohemian 抛出异常的方法不会阻止客户端在调用 getInstance() 之前反射性地调用构造函数。尽管它确保只创建一个实例,但不能保证这是由 Singleton 类的
getInstance()
方法完成的。访问控制检查将防止这种不需要的实例化。
Adapted from Making the Java Singleton Reflection Proof when using Lazy Loading:
The constructor has a check to see if the caller has access to it. As the link explains, a policy file that permits the Singleton class itself to call the constructor will need to be created.
Bohemian's method of throwing an exception does not prevent a client from reflectively calling the constructor before getInstance() is called. Even though it ensures that only one instance gets created, there's no guarantee that this is done by the Singleton class'
getInstance()
method.The access control check will prevent this unwanted instantiation.
我强烈建议阅读 什么是实现Java 中的单例模式? - 使用枚举会阻止您所描述的内容,并且是在 Java 中实现单例的推荐方法。
I highly recommend reading What is an efficient way to implement a singleton pattern in Java? - using an enum prevents what you're describing and is the recommended way to implement a singleton in java.
** 测试单例**
** Testing singleton**
如果您的单例实际上并不存储状态,那么您最好的选择是不使用单例。相反,将工厂实现为无静态方法。
If your singleton does not actually store state, then your best option is to not use a singleton. Instead, implement the factory as a static state-free method.