我在一个项目中看到了几种用法,如下所示(我发布了代码的精华,以便更容易阅读):
public static void main(Strings[] args) {
Injector i = Guice.createInjector(new MyModule());
i.getInstance(Foo.class);
}
class Foo {}
class MyModule extends AbstractModule {
@Provides
public Foo getFoo(Injector injector) {
return new
以下是我的例子:
use std::collections::HashMap;
fn main() {
let mut my_map = HashMap::new();
let mut my_vec = vec![5,6,7];
my_map.insert(my_vec, 4);
// This part is fine, so I can create HashMap
// with mutable Vector as key.
my_vec.push(8);
}
但是,我实际上不能修改向量!my_vec.push(8);会导致以
我注意到Guice可能会为provider注入代理,并将实际provider类的构造推迟到使用provider时。
public static class Foo {
private final Provider<Bar> barProvider;
@Inject
public Foo(Provider<Bar> barProvider) {
this.barProvider = barProvider;
}
public Bar getBar() {
return barProvider.get();
}
}
public
让我们声明"All Rocks have Minerals.":
public class Mineral
{
// Nevermind why a Mineral would have a GUID.
// This is just to show that each Mineral instance
// is universally-unique.
String guid;
@Inject
public Mineral(String id)
{
guid = id;
}
}
public
我希望能够使用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 a
假设我们有以下3个(顶级)类:
@ImplementedBy(MyClass.class)
public interface MyInterface {}
public class MyClass implements MyInterface {
@Inject
MyClass(MySecondClass mySecondClass) {}
}
public class MySecondClass {}
然后,以下代码将导致异常(A just-in-time binding to MySecondClass was already configured on a parent
我已经成功地创建了一个Guice binding注解来将单线程java.util.concurrent.ExecutorService实例注入到构造函数中。
下面是一个用法示例:
public class ContainsSingleThreadedExecutorService {
private final ExecutorService executorService;
@Inject
public ContainsSingleThreadedExecutorService(@SingleThreaded ExecutorService executorSe
我是依赖注入的新手,最近不得不学习Spring和Guice。下面的问题可能很蹩脚。
Spring允许您创建带有@Scope(" prototype ")注释的原型对象,这意味着每次返回一个新对象。
例如在我的弹簧容器里:-
@Scope("prototype")
@Bean
A getA(){
return new A();
}
一个新的对象A被注入到@autowired的所有引用中。
class B {
@Autowired
A objA;
}
然而,在guice喷射器中,同样的情况正在被注入。看起来guice只提供单例、请求或会话作用域。我的应
我正在做一个使用Depedency injection Google Guice Framework的项目。
可以将类与单例作用域绑定在一起,在Guice中为:
bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON);
如果FooImpl类本身是真正的单例类,如下所示:
public class FooImpl implements Foo{
public static final FooImpl INSTANCE = new FooImpl();
public static FooImpl getInstance(){..
我正在将一个为Java 7编写的应用程序升级到版本8,它使用Guice将配置值注入对象的构造函数。然而,在提高Java版本之后,我现在在运行单元测试时得到了这个神秘的错误:
java.lang.RuntimeException: com.google.inject.CreationException: Guice creation errors:
1) No implementation for java.util.Optional<java.lang.Boolean> annotated with @Config(value=AsyncHttpClientConfigProvi
我正试图在Guice的帮助下注入一个ArrayList of String。我想显示一个具有多个RadioButtons的面板(例如),用户可以在其中选择要激活的一些服务。
一旦被选中,我想获取所选服务的所有名称并将它们添加到列表中,并将此列表注入负责创建服务的经理。下面是一个示例:
public class UIProviderModule extends ProviderServiceModule {
private ArrayList<String> requestedServices;
public UIProviderModule(ArrayList&l
我有几种是这样的
// a value that is aware of its key type (K)
Bar<K>
// something that deals with such values and keys
Foo<V extends Bar<K>, K>
如何重新创建Foo,以便您可以在Guice中使用它?我坚持的一点是如何交叉引用K从Bar到第二个参数化类型Foo。
例如,
WildcardType kType = Types.subtypeOf(Object.class);
WildcardType barType =
Types