论坛首页 Java企业应用论坛

几个关于Guice的问题,关心Guice的请进

浏览 12145 次
精华帖 (2) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-15  
最近考虑在项目中使用guice,但是在替换一些Design patterns时遇到一些问题,特来这里向大家请教。

Guice的文档实在是太简单了,基本上就是几个简单的对象来回映射,对实际开发基本上没什么用。网上的例子也基本上停留在1+1=2的水平。

下面是我遇到的问题:

1. 如何在运行时态注入同一个对象的不同的实例?
   首先一个POJO:
 public class Foo {

    private boolean a;
    private boolean b;

    @inject
    public Foo(boolean a, boolean b) {
        this.a = a;
        this.b = b;
    }
} 


然后使用它:
public class FooContainer {

    @inject
    private Foo foo;

} 


我的问题是,Foo对象根据属性不同可以生成4中不同的实例,那么如何能够在runtime动态注入不同的实例呢?
另外,使用@Name注释可以绑定不同的实例,但是似乎无法动态绑定,而且如果类属性比较多的话,把每一种可能的实例都绑定起来,岂不是要吐血了。

2. 如何在runtime动态绑定任意对象?

 public class Foo {
           @inject
           private Object o;
     } 

这个例子里,Foo对象需要使用任意类型的对象实例,如何用Guice动态注入?类型为Object的对象实例为无限多,用@Name应该是解决不了问题了。

3. 如何解决传递对象问题?
   在API调用过程中,借助方法参数,可以将某一个对象实例在几个对象之间来回传递,而且它也不是singelton,这个被传递的对象不一定是Value Object。如果使用Guice的话如何实现?

4. 如何解决动态扩展的问题?
   为了便于理解,我举个简单的例子,MVC模式大家应该都很清楚,假如一个Control对应一个Model的话,用Guice可以完美实现。但是如果一个Control可以控制多个Model,而Model的数量是动态的,也就是说Control需要在runtime创建新的Model,同时还要保存已经存在的Models。针对这种情况,如何使用Guice注入?

5. 如何注入String?
   我刚刚开始接触Guice,估计用过Guice的朋友能够马上回答这个问题。先举个例子:
 public class Foo {

    @inject
    private String a;

    public Foo(String a) {
        this.a = a;
    }
} 


在使用Foo对象时,如何动态注入不同的String值?
其实这个问题和前面的几个问题类似,但是String是一个比较特殊的对象,所以单独拿出来提问。

6. 如何解决类关联的问题。例如:

public class Foo {

    private Bar bar;

    public Foo() {
        bar = new Bar(this);
    }

public class Bar {

    private Foo foo;

    public Bar(Foo foo) {
      this.foo = foo;
    }

如何保证使用guice注入后,bar中的foo是创建Bar的那个Foo对象实例?
   发表时间:2008-04-15  
大家都对Guice不感兴趣?
0 请登录后投票
   发表时间:2008-04-15  
Guice是强类型的IOC,这个看看Guice的Key就应该知道了.

A Key uniquely identifies each binding. The key consists of a type which the client depends on and an optional annotation. You can use an annotation to differentiate multiple bindings to the same type. The key's type and annotation correspond to the type and annotation at a point of injection.

1.这个问题在Guice下估计只能这样了(Spring的IOC要解决这个问题也得一一列举),
不清楚你的动态绑定是什么意思,可以看看Binder 和 Provider
2.你从任意对象中选择一个吗?如果是的话可以用Module机制直接bind(当然注解是少不了的)
3.不明白什么意思
4.大不了直接用注入Provider,什么时候要用再取得
5.看 http://docs.google.com/View?docid=dd2fhx4z_5df5hw8 的 [Injecting Constant Values]


PS:你的这些问题Guice的文档中其实都可以找到解决方法,Guice的文档还是比较可以的.
0 请登录后投票
   发表时间:2008-04-15  
yujiang 写道
Guice是强类型的IOC,这个看看Guice的Key就应该知道了.

A Key uniquely identifies each binding. The key consists of a type which the client depends on and an optional annotation. You can use an annotation to differentiate multiple bindings to the same type. The key's type and annotation correspond to the type and annotation at a point of injection.

1.这个问题在Guice下估计只能这样了(Spring的IOC要解决这个问题也得一一列举),
不清楚你的动态绑定是什么意思,可以看看Binder 和 Provider
2.你从任意对象中选择一个吗?如果是的话可以用Module机制直接bind(当然注解是少不了的)
3.不明白什么意思
4.大不了直接用注入Provider,什么时候要用再取得
5.看 http://docs.google.com/View?docid=dd2fhx4z_5df5hw8 的 [Injecting Constant Values]


PS:你的这些问题Guice的文档中其实都可以找到解决方法,Guice的文档还是比较可以的.


先谢谢回答我的问题。

1. 我主要是想问如何在runtime注入不同的实例。比方说有两个Foo实例:
Foo foo1 -> Foo(true, true)
Foo foo2 -> Foo(false, false)

然后我在程序中不同的地方获得两个FooContainer对象,如何实现在一个FooContainer对象中注入foo1,而在另外一个FooContainer对象里面注入foo2?
2. 没明白你的解释,什么注解? @Name?
5. 我举的这个例子里String也是需要动态注入的,不是constant。例如:
 public class FooContainer {
    @inject
    private Foo foo1;
} 

我希望在FooContainer里注入一个Foo,但是Foo里的String只有在runtime才能确定,用Guice怎么处理?
0 请登录后投票
   发表时间:2008-04-15  
写了点代码(不考虑Scope),估计会有点用.

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.inject.Binder;
import com.google.inject.BindingAnnotation;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.google.inject.name.Names;

public class G {

	public static class Foo {
		boolean a;
		boolean b;

		@Inject
		@Named("random")
		Integer c;

		public Foo() {
		}

		public Foo(boolean a, boolean b) {
			this.a = a;
			this.b = b;
		}
	}

	public static class FooContainer {
		Foo foo = null;
	}

	@Target( { METHOD, CONSTRUCTOR, FIELD })
	@Retention(RUNTIME)
	@BindingAnnotation
	public static @interface A {

	}

	@Target( { METHOD, CONSTRUCTOR, FIELD })
	@Retention(RUNTIME)
	@BindingAnnotation
	public static @interface B {

	}

	@Inject
	@A
	FooContainer containerA = null;

	@Inject
	@B
	FooContainer containerB = null;

	public static void main(String[] args) {

		Injector injector = Guice.createInjector(new Module() {

			@Override
			public void configure(Binder binder) {

				binder.bind(Integer.class).annotatedWith(Names.named("random"))
						.toProvider(new Provider<Integer>() {

							AtomicInteger integer = new AtomicInteger(0);

							@Override
							public Integer get() {
								return integer.incrementAndGet();
							}
						});

				binder.bind(Foo.class).annotatedWith(Names.named("true-false"))
						.toInstance(new Foo(true, false));

				binder.bind(Key.get(Foo.class, Names.named("false-true")))
						.toProvider(new Provider<Foo>() {

							Foo foo = null;

							@Override
							public Foo get() {

								CHECK(foo.c != null, "foo.c != null");
								return foo;
							}

							@Inject
							public void setFoo(Foo foo) {

								this.foo = foo;
								foo.a = false;
								foo.b = true;
							}
						});

				binder.bind(Key.get(FooContainer.class, A.class)).toProvider(
						new Provider<FooContainer>() {
							
							@Inject
							@Named("true-false")
							Foo foo = null;

							@Inject
							FooContainer raw = null;

							@Override
							public FooContainer get() {

								raw.foo = foo;
								return raw;
							}
						});

				binder.bind(Key.get(FooContainer.class, B.class)).toProvider(
						new Provider<FooContainer>() {

							@Inject
							@Named("false-true")
							Foo foo = null;

							@Inject
							FooContainer raw = null;

							@Override
							public FooContainer get() {

								raw.foo = foo;
								return raw;
							}
						});

			}
		});

		G g = injector.getInstance(G.class);

		CHECK(g.containerA != g.containerB, "g.containerA != g.containerB");

		CHECK(g.containerA.foo.a == true, "g.containerA.foo.a == true");
		CHECK(g.containerA.foo.b == false, "g.containerA.foo.b == false");

		CHECK(g.containerB.foo.a == false, "g.containerB.foo.a == false");
		CHECK(g.containerB.foo.b == true, "g.containerB.foo.b == true");
	}

	static void CHECK(boolean r, String msg) {

		if (!r)
			throw new RuntimeException(msg == null ? "" : "failed: " + msg);
	}
}
0 请登录后投票
   发表时间:2008-04-15  
ajoo说过一次,guice属于静态绑定,似乎做不到动态注入。

如果需要动态注入什么东西,可以用用一个Provider,动态放到provider里,然后再provider.get()取出来。
一个接口多个实例就是靠@Named
import com.google.inject.*;
import com.google.inject.name.Named;
import static com.google.inject.name.Names.*;

public class Main {

    @Inject
    @Named("blue")
    private Service blueService;

    @Inject
    @Named("green")
    private Service greenService;

    public static void main(String[] args) {
        Main main = Guice.createInjector(new Module() {
            public void configure(Binder binder) {
                binder.bind(Service.class).annotatedWith(named("blue"))
                                        .to(BlueServiceImpl.class);
                binder.bind(Service.class).annotatedWith(named("green"))
                                        .to(GreenServiceImpl.class);
            }
        }).getInstance(Main.class);

        main.blueService.hello();
        main.greenService.hello();
    }
}



String应该是可以当作constants常量直接注入的。

我们把guice的文档整理了一下,你可以看看:

http://www.family168.com/tutorial/guice/html/
0 请登录后投票
   发表时间:2008-04-15  
非常感谢,写了这么一大堆。:-)

这种做法和我考虑的一样,问题是这样也太麻烦了吧!
只是 new FooContainer(new Foo(true, false))和new FooContainer(new Foo(false, true))就能搞定的,结果用Guice写了这么多代码。

还有就是关于String的那个问题还存在。我再举例说明一下。在程序的某处需要Foo对象,例如Foo("ABC"), 过段时间,在其他地方又需要Foo("DEF"), 再过段时间,又需要Foo("XZY")就这样继续下去。。。。。。

我怎么用一个Foo类类实现这些映射?这些String之间并没有任何规律可循。
0 请登录后投票
   发表时间:2008-04-15  
polygoncell 写道
非常感谢,写了这么一大堆。:-)

这种做法和我考虑的一样,问题是这样也太麻烦了吧!
只是 new FooContainer(new Foo(true, false))和new FooContainer(new Foo(false, true))就能搞定的,结果用Guice写了这么多代码。

还有就是关于String的那个问题还存在。我再举例说明一下。在程序的某处需要Foo对象,例如Foo("ABC"), 过段时间,在其他地方又需要Foo("DEF"), 再过段时间,又需要Foo("XZY")就这样继续下去。。。。。。

我怎么用一个Foo类类实现这些映射?这些String之间并没有任何规律可循。

考虑用FooFactory?其实你最好写一个不用Guice的话,你手工会怎么写,这样可以帮助大家理解。
0 请登录后投票
   发表时间:2008-04-15  
ajoo 写道
polygoncell 写道
非常感谢,写了这么一大堆。:-)

这种做法和我考虑的一样,问题是这样也太麻烦了吧!
只是 new FooContainer(new Foo(true, false))和new FooContainer(new Foo(false, true))就能搞定的,结果用Guice写了这么多代码。

还有就是关于String的那个问题还存在。我再举例说明一下。在程序的某处需要Foo对象,例如Foo("ABC"), 过段时间,在其他地方又需要Foo("DEF"), 再过段时间,又需要Foo("XZY")就这样继续下去。。。。。。

我怎么用一个Foo类类实现这些映射?这些String之间并没有任何规律可循。

考虑用FooFactory?其实你最好写一个不用Guice的话,你手工会怎么写,这样可以帮助大家理解。


我是想尽量简化问题,这样大家讨论起来比较方便。

手动写就是 new Foo("any String"), 什么地方需要了,就创建一个。问题是参数可以是任何String,无法预先定义。

用FooFactory? 你的意思是@inject FooFactory,然后让FooFactory创建Foo对象?不是所有对象创建的工作都应该交给Guice来完成么?这样做是不是有些背离了Guice了?
0 请登录后投票
   发表时间:2008-04-15  
ajoo 写道
polygoncell 写道
非常感谢,写了这么一大堆。:-)

这种做法和我考虑的一样,问题是这样也太麻烦了吧!
只是 new FooContainer(new Foo(true, false))和new FooContainer(new Foo(false, true))就能搞定的,结果用Guice写了这么多代码。

还有就是关于String的那个问题还存在。我再举例说明一下。在程序的某处需要Foo对象,例如Foo("ABC"), 过段时间,在其他地方又需要Foo("DEF"), 再过段时间,又需要Foo("XZY")就这样继续下去。。。。。。

我怎么用一个Foo类类实现这些映射?这些String之间并没有任何规律可循。

考虑用FooFactory?其实你最好写一个不用Guice的话,你手工会怎么写,这样可以帮助大家理解。


你這種問題,注入是不能解決問題的,因未注入不是解決這個問題的,因未在注入的時候,還沒有你說的Foo(“abc”)這些實例的存在,
解決這個問題,你可以運用state模式,不過更好的方法,還是if+else,(或者不要else)
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics