PicoContainer似乎支持JSR-330、@Inject和@Named。(不,另一个问题似乎没有帮助,因为它没有解决PicoContainer站点说已经添加了对JSR-330的一些支持这一事实。)
我在容器中添加了如下内容:
container.addComponent(Foo.class);
container.addComponent("myBar", new MySpecialBar());
container.addComponent("decoy", new SomeOtherBar());我用@Named标记构造函数参数
public class Foo(@Named("myBar") Bar bar) { ...但是当我尝试获取一个Foo实例时,PicoContainer抱怨说它有太多的Bar实例可供选择。
问题1:如何使PicoContainer与构造函数注入的@Named一起工作?
然后我尝试在Foo.java中使用字段注入。
@Inject
@Named("myBar")
Bar bar;这也没用。
问题2:如何让PicoContainer与@Inject和@Named一起使用构造函数注入?
或者PicoContainer新闻页面是错的,根本就没有对JSR-330的任何PicoContainer 2.x支持?
发布于 2015-01-16 10:09:20
看起来我是堆栈溢出中唯一一个回答pico问题的人,但我不是pico团队成员,所以对于最后的答案,您可能需要访问他们的邮件列表:)
在查看框架源代码(2.10 . 2.15)时,我看不到对javax.inject.Named的任何支持,@Inject被支持为pico注释,而不是javax.inject.Inject
至于解决不明确的依赖关系,pico提供了几种方法:http://picocontainer.codehaus.org/ambiguous-injectable-help.html (使用参数名称,IMHO有点奇怪,但对您来说可能还可以)和http://picocontainer.codehaus.org/disambiguation.html (使用参数对象-不错,但很详细,另一种使用绑定注释的方法,如Guice,IMHO甚至更奇怪),如果以上任何一种方法都不适合您,您可以采用参数对象概念,并使用IMHO更干净的外观,使您少使用消歧适配器。
class DisambiguationAdapter<T> extends AbstractAdapter<T> {
private final Object[] params;
public DisambiguationAdapter(Object componentKey, Class<T> componentImplementation, Object... params) {
super(componentKey, componentImplementation);
this.params = params;
}
// the idea is to make child container that overrides ambiguos deps using the parameters hints
@Override
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
MutablePicoContainer tmpContainer = new DefaultPicoContainer(container);
tmpContainer.addComponent(getComponentKey(), getComponentImplementation());
for (Object param : params) {
tmpContainer.addComponent(container.getComponent(param));
}
T instance = tmpContainer.getComponent(getComponentImplementation());
tmpContainer.dispose();
return instance;
}
@Override
public void verify(PicoContainer container) throws PicoCompositionException {
for (Object param : params) {
if(container.getComponent(param) == null) {
throw new PicoCompositionException("Can't resolve param-key=" + param + " to satisfy dependencies for " + getDescriptor());
}
}
}
@Override
public String getDescriptor() {
return getComponentImplementation().getCanonicalName();
}
}然后声明此方法以添加需要模糊dep的组件:
public void register(Object key, Class<?> component, Object... params) {
container.addAdapter(new DisambiguationAdapter<>(key, component, params));
}然后你就这样用它:
// constructor for some class requiring ambig deps
public ProfileFinder(/* ambig param */ ProfileDao dao, /* any other params */ Param1 param1, Param1 param2)......
// container composition
// hint key, impl class
c.addComponent(ProfileDaoSelector.SLAVE, jdbi.onDemand(ProfileDao.class));
c.addComponent(ProfileDaoSelector.MASTER, jdbiMaster.onDemand(ProfileDao.class));
// impl class, hint keys array
c.register(ProfileService.class, new Object[] {ProfileDaoSelector.MASTER});
c.register(ProfileFinder.class, new Object[] {ProfileDaoSelector.SLAVE});https://stackoverflow.com/questions/27975607
复制相似问题