如何使用PicoContainer管理动态依赖项?

时间:2022-06-24 07:15:12

Say I have two classes A and B, with B depending on A.

假设我有两个A和B类,B取决于A.

public class A {}

public class B {
    public B(A a) {}
}

It's easy to resolve B in a single PicoContainer.

在单个PicoContainer中解析B很容易。

    final MutablePicoContainer root = new PicoBuilder().build();
    root.addComponent(new A());
    root.addComponent(B.class, B.class);
    System.out.println(root.getComponent(B.class));

But I'd like to have different instances of B for different sessions, with variable instances of A. I'm thinking about something like this.

但是我想为不同的会话设置不同的B实例,使用A的变量实例。我正在考虑这样的事情。

    // during startup
    final MutablePicoContainer root = new PicoBuilder().build();
    root.addComponent(B.class, B.class);

    // later, initialize sessions
    final MutablePicoContainer session = new PicoBuilder(root)
        .implementedBy(TransientPicoContainer.class)
        .build();
    session.addComponent(new A());

    // some request
    System.out.println(session.getComponent(B.class));

Above code isn't working, because when asking session for a B it asks the parent container root for it. B is found there, but resolved only within root and its parents, leading to an UnsatisfiableDependenciesException.

上面的代码不起作用,因为在询问B的会话时,它会询问父容器根。在那里找到B,但只在root及其父项中解析,导致UnsatisfiableDependenciesException。

Is there any good way to make this work? Or is this an anti-pattern, and I'm solving the problem in the wrong way?

有没有什么好办法让这项工作?或者这是一种反模式,我是以错误的方式解决问题的?

3 个解决方案

#1


I would register B within the session container as well. Any other dependency of B you can leave in the root container. Assume B has another dependency on C. So you can do the following:

我也会在会话容器中注册B. B的任何其他依赖关系都可以保留在根容器中。假设B对C有另一个依赖关系。所以你可以做以下事情:

// during startup
final MutablePicoContainer root = new PicoBuilder().build();
root.addComponent(C.class, C.class);

// later, initialize sessions
final MutablePicoContainer session = new PicoBuilder(root)
    .implementedBy(TransientPicoContainer.class)
    .build();
session.addComponent(B.class, B.class);
session.addComponent(new A());

// some request
System.out.println(session.getComponent(B.class));

#2


Solving a performance problem that doesn't exist isn't a good approach. Have you done any profiling to verify the problem?

解决不存在的性能问题不是一个好方法。你做过任何分析来验证问题吗?

If not, consider doing that first.

如果没有,请考虑先做。

#3


Did you enable caching on your container (or are you using Pico 1.x)?

您是否在容器上启用了缓存(或者您使用的是Pico 1.x)?

Try reading this, maybe disabling the cache is enough to solve this problem.

尝试阅读此内容,可能禁用缓存足以解决此问题。

#1


I would register B within the session container as well. Any other dependency of B you can leave in the root container. Assume B has another dependency on C. So you can do the following:

我也会在会话容器中注册B. B的任何其他依赖关系都可以保留在根容器中。假设B对C有另一个依赖关系。所以你可以做以下事情:

// during startup
final MutablePicoContainer root = new PicoBuilder().build();
root.addComponent(C.class, C.class);

// later, initialize sessions
final MutablePicoContainer session = new PicoBuilder(root)
    .implementedBy(TransientPicoContainer.class)
    .build();
session.addComponent(B.class, B.class);
session.addComponent(new A());

// some request
System.out.println(session.getComponent(B.class));

#2


Solving a performance problem that doesn't exist isn't a good approach. Have you done any profiling to verify the problem?

解决不存在的性能问题不是一个好方法。你做过任何分析来验证问题吗?

If not, consider doing that first.

如果没有,请考虑先做。

#3


Did you enable caching on your container (or are you using Pico 1.x)?

您是否在容器上启用了缓存(或者您使用的是Pico 1.x)?

Try reading this, maybe disabling the cache is enough to solve this problem.

尝试阅读此内容,可能禁用缓存足以解决此问题。