标签: ioc-container

春天自我注射

我尝试使用Spring 3.x的以下代码失败了BeanNotFoundException,它应该根据我之前问过的问题的答案 - 我可以使用Spring注入相同的类吗?

@Service
public class UserService implements Service{
    @Autowired
    private Service self;
}
Run Code Online (Sandbox Code Playgroud)

自从我用Java 6尝试这个以来,我发现以下代码工作正常:

@Service(value = "someService")
public class UserService implements Service{
    @Resource(name = "someService")
    private Service self;
}
Run Code Online (Sandbox Code Playgroud)

但我不明白它如何解决循环依赖.

编辑:
这是错误消息.OP在其中一个答案的评论中提到它:

由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.spring.service.Service]的匹配bean:期望至少有一个bean可以作为此依赖项的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

java spring dependency-injection ioc-container

58
推荐指数
2
解决办法
3万
查看次数

温莎 - 从容器中拉出瞬态物体

如何从容器中拉出瞬态物体?我是否必须在容器中注册它们并注入需要类的构造函数?将所有内容注入构造函数中感觉不太好.也只是为了一个类,我不想创建一个TypedFactory并将工厂注入需要的类.

我想到的另一个想法是根据需要"新"起来.但我也在我的Logger所有类中注入一个组件(通过属性).因此,如果我新建它们,我将不得不手动实例化Logger这些类.如何继续为我的所有课程使用容器?

记录器注入:我的大多数类都Logger定义了属性,除非存在继承链(在这种情况下,只有基类具有此属性,并且所有派生类都使用该属性).当这些通过Windsor容器实例化时,它们会将我的实现ILogger注入其中.

//Install QueueMonitor as Singleton
Container.Register(Component.For<QueueMonitor>().LifestyleSingleton());
//Install DataProcessor as Trnsient
Container.Register(Component.For<DataProcessor>().LifestyleTransient());

Container.Register(Component.For<Data>().LifestyleScoped());

public class QueueMonitor
{
    private dataProcessor;

    public ILogger Logger { get; set; }

    public void OnDataReceived(Data data)
    {
        //pull the dataProcessor from factory    
        dataProcessor.ProcessData(data);
    }
}

public class DataProcessor
{
    public ILogger Logger { get; set; }

    public Record[] ProcessData(Data data)
    {
        //Data can have multiple Records
        //Loop through the data and create new set …
Run Code Online (Sandbox Code Playgroud)

.net dependency-injection castle-windsor ioc-container

57
推荐指数
1
解决办法
2万
查看次数

.NET Core DI,将参数传递给构造函数的方法

拥有以下服务构造函数

public class Service : IService
{
     public Service(IOtherService service1, IAnotherOne service2, string arg)
     {

     }
}
Run Code Online (Sandbox Code Playgroud)

使用.NET Core IOC机制传递参数有哪些选择?

_serviceCollection.AddSingleton<IOtherService , OtherService>();
_serviceCollection.AddSingleton<IAnotherOne , AnotherOne>();
_serviceCollection.AddSingleton<IService>(x=>new Service( _serviceCollection.BuildServiceProvider().GetService<IOtherService>(), _serviceCollection.BuildServiceProvider().GetService<IAnotherOne >(), "" ));
Run Code Online (Sandbox Code Playgroud)

还有其他方法吗?

c# dependency-injection ioc-container .net-core asp.net-core

52
推荐指数
4
解决办法
2万
查看次数

比较Castle Windsor,Unity和StructureMap

在跟进基耶斯洛夫的说法温莎确实比其他国际奥委会的多很多,我想了解这些国际奥委会如何叠起来反对对方,这温莎城堡带来的好处/附加设施.

有比较吗?有人可以帮助我理解Castle Windsor提供的其他IoC附加功能

structuremap dependency-injection castle-windsor ioc-container unity-container

51
推荐指数
2
解决办法
3万
查看次数

创建单例来访问统一容器或通过应用程序传递它是否更好?

我正在使用IoC框架,我选择使用Unity.我还没有完全理解的一件事是如何更深入地解析应用程序中的对象.我怀疑我当时还没有灯泡可以说清楚.

因此,我尝试在psuedo'ish代码中执行以下操作

void Workflow(IUnityContatiner contatiner, XPathNavigator someXml)
{
   testSuiteParser = container.Resolve<ITestSuiteParser>
   TestSuite testSuite = testSuiteParser.Parse(SomeXml) 
   // Do some mind blowing stuff here
}
Run Code Online (Sandbox Code Playgroud)

所以testSuiteParser.Parse执行以下操作

TestSuite Parse(XPathNavigator someXml)
{
    TestStuite testSuite = ??? // I want to get this from my Unity Container
    List<XPathNavigator> aListOfNodes = DoSomeThingToGetNodes(someXml)

    foreach (XPathNavigator blah in aListOfNodes)
    {
        //EDIT I want to get this from my Unity Container
        TestCase testCase = new TestCase() 
        testSuite.TestCase.Add(testCase);
    } 
}
Run Code Online (Sandbox Code Playgroud)

我可以看到三个选项:

  1. 创建一个Singleton来存储我可以在任何地方访问的Unity容器.我真的不喜欢这种方法.添加这样的依赖项来使用依赖注入框架似乎有点奇怪.
  2. 将IUnityContainer传递给我的TestSuiteParser类及其中的每个子类(假设它是n级深度或实际上大约3级深度).在任何地方传递IUnityContainer只是看起来很奇怪.我可能只需要克服这一点.
  3. 在正确的方式上使用Unity的灯泡时刻.希望有人可以帮助轻弹开关.

[编辑]我不清楚的一件事是我想为foreach语句的每次迭代创建一个新的测试用例实例.上面的示例需要解析测试套件配置并填充测试用例对象的集合

c# ioc-container unity-container

49
推荐指数
2
解决办法
2万
查看次数

MVC,EF - Unity中的DataContext单例实例Per-Web-Request

我有一个MVC 3 Web应用程序,我使用Entity Framework进行数据访问.此外,我简单地使用了存储库模式,例如,所有与产品相关的东西都在"ProductRepository"中处理,所有与User相关的东西都在"UserRepository"中处理.

因此,我使用UNITY容器来创建DataContext的单例实例,我将其注入每个存储库.快速搜索Google,每个人都建议您不要使用DataContext的单例实例,因为它可能会在将来给您带来一些内存泄漏.

所以,受这篇文章的启发,为每个Web请求创建一个DataContext的单例实例就是答案(如果我错了,请纠正我!)

http://blogs.microsoft.co.il/blogs/gilf/archive/2010/05/18/how-to-manage-objectcontext-per-request-in-asp-net.aspx

但是,UNITY不支持"Per-web-request"终身经理.但是,可以实现自己的自定义生命周期管理器,它可以为您处理此问题.实际上,这篇文章对此进行了讨论:

Unity中的Singleton Per Call上下文(Web请求)

问题是,我现在已经实现了上面帖子中描述的自定义生命周期管理器,但我不确定这是否是这样做的方法.我也想知道在提供的解决方案中处理datacontext实例的位置?我错过了什么吗?

实际上有更好的方法来解决我的"问题"吗?

谢谢!

**添加了有关我的实施的信息**

以下是我的Global.asax,Controller和Repository的片段.这清楚地说明了我的实施情况.

Global.asax中

  var container = new UnityContainer();
            container
                .RegisterType<ProductsRepository>(new ContainerControlledLifetimeManager())
                .RegisterType<CategoryRepository>(new ContainerControlledLifetimeManager())
                .RegisterType<MyEntities>(new PerResolveLifetimeManager(), dbConnectionString)
Run Code Online (Sandbox Code Playgroud)

调节器

private ProductsRepository _productsRepository;
private CategoryRepository _categoryRepository;

public ProductsController(ProductsRepository productsRepository, CategoryRepository categoryRepository)
{
   _productsRepository = productsRepository;
   _categoryRepository = categoryRepository;
}

public ActionResult Index()
{
   ProductCategory category = _categoryRepository.GetProductCategory(categoryId);
   . 
   . 
   . 
}

protected override void Dispose(bool disposing)
{
    base.Dispose(disposing);
    _productsRepository.Dispose();
    _categoryRepository.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

产品库

public class ProductsRepository : IDisposable
{

private …
Run Code Online (Sandbox Code Playgroud)

entity-framework ioc-container unity-container asp.net-mvc-3

49
推荐指数
3
解决办法
3万
查看次数

NInject:你在哪里保留对内核的引用?

我在一个新的Web应用程序上使用NInject,有两件事我不清楚:

  1. 我是否需要保持对内核(Session/App变量)的引用以确保GC不收集我的所有实例?例如,如果我指定.Using()然后收集Kernel对象,那么我的所有"单身人士"都不是被收集的吗?

  2. 如果我确实需要保持对Kernel对象的引用,我如何允许传入WithArguments()的参数改变或者这是不可能的.

c# dependency-injection ninject ioc-container inversion-of-control

48
推荐指数
2
解决办法
1万
查看次数

Ninject + Bind通用存储库

我正在尝试将一个通用的IRepository <>接口绑定到我的通用存储库<> - 但它总是返回null?

我尝试过各种各样的事情:

Bind(typeof(IRepository<CustomerModel>)).To(typeof(Repository<CustomerModel>)); 
Bind(typeof(IRepository<>)).To(typeof(Repository<>)); 
Run Code Online (Sandbox Code Playgroud)

但是,如果我传入非通用接口和类,那么它就像梦一样?

c# ninject ioc-container

48
推荐指数
1
解决办法
2万
查看次数

使用Guice注入通用实现

我希望能够使用Guice注入通用接口的通用实现.

public interface Repository<T> {
  void save(T item);
  T get(int id);
}

public MyRepository<T> implements Repository<T> {
  @Override
  public void save(T item) {
    // do saving
    return item;
  }
  @Override
  public T get(int id) {
    // get item and return
  }
}
Run Code Online (Sandbox Code Playgroud)

在C#中使用Castle.Windsor,我可以做到:

Component.For(typeof(Repository<>)).ImplementedBy(typeof(MyRepository<>))
Run Code Online (Sandbox Code Playgroud)

但我不认为Guice中存在等价物.我知道我可以TypeLiteral在Guice 中使用来注册个人实现,但是有没有办法像Windsor那样一次注册它们?

编辑:

这是一个用法示例:

Injector injector = Guice.createInjector(new MyModule());
Repository<Class1> repo1 = injector.getInstance(new Key<Repository<Class1>>() {});
Repository<Class2> repo2 = injector.getInstance(new Key<Repository<Class2>>() {});
Run Code Online (Sandbox Code Playgroud)

虽然更可能的用法是注入另一个类:

public class ClassThatUsesRepository {
  private Repository<Class1> repository;

  @Inject
  public ClassThatUsesRepository(Repository<Class1> …
Run Code Online (Sandbox Code Playgroud)

java ioc-container guice typeliteral

47
推荐指数
2
解决办法
3万
查看次数

为什么MVC4使用服务定位器反模式?

在阅读Mark Seemann的".NET中的依赖注入"后,我远离服务定位器,这是一种反模式.

在阅读MVC 4上的发行说明后,我看到:

通过DependencyResolver改进了控制反转(IoC):Web API现在使用MVC依赖解析器实现的服务定位器模式来获取许多不同设施的实例.

因此,我对于微软在2012年使用服务定位器的原因感到好奇和困惑.

dependency-injection ioc-container service-locator asp.net-mvc-4

46
推荐指数
2
解决办法
1万
查看次数