通常,我喜欢让应用程序完全不知道IoC容器.但是我遇到了需要访问它的问题.为了消除痛苦,我使用了一个基本的单身人士.在你跑山或拔出霰弹枪之前,让我回顾一下我的解决方案.基本上,IoC单例绝对没有任何东西,它只是委托给必须传入的内部接口.我发现这使得使用Singleton减少痛苦.
下面是IoC包装器:
public static class IoC
{
private static IDependencyResolver inner;
public static void InitWith(IDependencyResolver container)
{
inner = container;
}
/// <exception cref="InvalidOperationException">Container has not been initialized. Please supply an instance if IWindsorContainer.</exception>
public static T Resolve<T>()
{
if ( inner == null)
throw new InvalidOperationException("Container has not been initialized. Please supply an instance if IWindsorContainer.");
return inner.Resolve<T>();
}
public static T[] ResolveAll<T>()
{
return inner.ResolveAll<T>();
}
}
Run Code Online (Sandbox Code Playgroud)
的IDependencyResolver:
public interface IDependencyResolver
{
T Resolve<T>();
T[] ResolveAll<T>();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经取得了很大的成功,我曾经几次使用它(也许每一个项目一次,我真的不想一直使用它),因为我可以注入任何我想要的东西:城堡,Stub,假货,等等 …
我在许多地方读到单身人士可以使用接口.有些我怎么也无法理解这一点.
Python是否拥有所有字符串的池并且它们是(字符串)单例吗?
更准确地说,在下面的代码中,在内存中创建了一个或两个字符串:
a = str(num)
b = str(num)
Run Code Online (Sandbox Code Playgroud)
?
我有一个存储库管理器来管理我的存储库.我有@Autowired来实例化我的属性,但它们总是为空.在我的xml中正确配置了bean.有什么理由吗?
public class RepositoryManager {
private static RepositoryManager instance;
private RepositoryManager()
{
}
public static RepositoryManager Instance()
{
if(instance == null)
instance = new RepositoryManager();
return instance;
}
@Autowired
private IUserRepository userRepository;
@Autowired
private IRoleRepository roleRepository;
@Autowired
private IAssetRepository assetRepository;
public IUserRepository getUserRepository() {
return userRepository;
}
public void setUserRepository(IUserRepository userRepository) {
this.userRepository = userRepository;
}
public IRoleRepository getRoleReposttory() {
return roleRepository;
}
public void setRoleReposttory(IRoleRepository roleRepository) {
this.roleRepository = roleRepository;
}
public IAssetRepository getAssetRepository() {
return assetRepository;
}
public …Run Code Online (Sandbox Code Playgroud) 这是我一直用于工厂的基本模式的示例,它返回一个线程安全的Singleton:
public class UserServiceFactory {
private volatile static UserService userService;
private UserServiceFactory() { }
public static UserService getInstance() {
if (userService == null) {
synchronized(UserServiceImpl.class) {
if (userService == null) {
userService = new UserServiceImpl();
}
}
}
return userService;
}
}
Run Code Online (Sandbox Code Playgroud)
它使用volatile和double check惯用法来确保创建单个实例并在线程中可见.
是否有更简洁和/或更便宜的方式来实现1.6+中的相同目标.
我有一个EJB,它需要是一个单独的和有状态的,因为它将成为一种连接池.我的问题是:
@Singleton注释定义EJB,那么默认情况下它是否为有状态,还是我必须同时定义它@Stateful?@Stateless注释吗?试图找到一些关于此的文件,但到目前为止没有运气,所以有知识的人请分享你的智慧,也许还有一两个链接.
在问题中什么是在Java中实现单例模式的有效方法?最赞成的回答是,使用Enum来实现单例.
这很好,我理解这些论点,分别是语言优势.
但是,我有一组我定义单例但需要扩展其他类的类,这对于枚举方法是不可能的,因为枚举不能子类化.
约书亚布洛赫在他的幻灯片中说:
- 但是缺少一件事 - 你无法扩展枚举类型
- 在大多数情况下,你不应该
- 一个引人注目的用例操作代码
在大多数情况下你不应该:有人可以详细说明吗?我已经实现了几个servlet并扩展了HttpServlet,为什么这些不应该是单例?我只想在我的应用程序中使用它们的一个实例.
给定两个模块,main和x,具有以下内容:
主要:
class Singleton(object):
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
cls._instance.x = 10
return cls._instance
uvw = Singleton()
if __name__ == "__main__":
print(id(uvw))
uvw.x += 10
print(uvw.x)
import x
Run Code Online (Sandbox Code Playgroud)
和x,分别为:
import main
print(id(main.uvw))
print(main.uvw.x)
Run Code Online (Sandbox Code Playgroud)
我现在希望执行main会在两个实例中产生相同的ID和值20,但我得到的是:
$ python main.py
140592861777168
20
140592861207504
10
Run Code Online (Sandbox Code Playgroud)
有什么方法可以确保uvw两个地方都是同一个物体吗?
我有2个选择:
单身模式
class Singleton{
private static Singleton singleton = null;
public static synchronized Singleton getInstance(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
}
Run Code Online (Sandbox Code Playgroud)使用一个static final字段
private static final Singleton singleton = new Singleton();
public static Singleton getSingleton() {
return singleton;
}
Run Code Online (Sandbox Code Playgroud)有什么不同?(单线程或多线程)
更新:我知道Bill Pugh或enum方法.我不是在寻找正确的方法,但我只使用过1.在1或2中是否有任何差异?
我有一个使用单例类的Windows服务ThreadQueue<T>.当服务启动时,它会调用ThreadQueue<string>.Start()此类,然后接受并将限制并发性的任务排队到可配置数量的线程.
ThreadQueue<string>.Start() 在服务启动时调用一次且仅调用一次.
有时,在运行几个小时后,我收到以下异常:
Application: myservice.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
at Apollo.Business.Framework.Threading.ThreadQueue.ThreadQueue`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Start()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
Run Code Online (Sandbox Code Playgroud)
System.__Canon将此调用作为类型参数传递给它的是什么和什么?
谁能摆脱任何光明?