Guice是一个轻量级IOC容器(AOP也支持)
注入的配置是自写的Java类,必须继承AbstractModule抽象类,实现configure()方法。
public class HumanModule extends AbstractModule {
@Override
protected void configure() {
bind(Human.class).to(Child.class);
bind(HumanGroup.class);
bind(String.class).annotatedWith(FixedValue.class).toInstance("*_*Childs*_*");
bind(Human.class).annotatedWith(FixedValue.class).to(Woman.class).in(Singleton.class);
}
}
根据以上代码介绍几种注入
1. 直接注入具体类
bind(HumanGroup.class);
2. 基于接口注入实现类
bind(Human.class).to(Child.class);
3. 注入基础类型的值
bind(String.class).annotatedWith(FixedValue.class).toInstance("*_*Childs*_*");
4. 根据Annotation注入不同类型的实现类
bind(Human.class).annotatedWith(FixedValue.class).to(Woman.class).in(Singleton.class);
5. 可设置对象实体的生命周期(单例)
bind(Human.class).annotatedWith(FixedValue.class).to(Woman.class).in(Singleton.class);
具体例子设计如下:
接口Human类,里面有talk方法,Woman和Child分别为实现类,HumanModule为Guice的配置,HumanGroup为调用并执行talk的具体类。可以执行MainTest里的main方法测试这些例子。
Human类
public interface Human {
public void talk(String name);
}
Woman类
public class Woman implements Human {
@Override
public void talk(String name) {
System.out.println(name+":I want a house.");
}
}
Child类
public class Child implements Human {
@Override
public void talk(String name) {
System.out.println(name+":WaWaWa~~(Cry)");
}
}
HumanModule类
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
public class HumanModule extends AbstractModule {
@Override
protected void configure() {
bind(Human.class).to(Child.class);
bind(HumanGroup.class);
bind(String.class).annotatedWith(FixedValue.class).toInstance("*_*Childs*_*");
bind(Human.class).annotatedWith(FixedValue.class).to(Woman.class).in(Singleton.class);
}
}
HumanGroup类
import java.util.Date;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class HumanGroup {
public HumanGroup() {
System.out.println(new Date().getTime());// test singleton
}
@Inject
@FixedValue
private String groupName;
/**
* properties inject
*/
@Inject
private Human h1;
public void h1talk() {
h1.talk("h1");
}
private Human h2;
/**
* setter inject
*/
@Inject
public void setH2(Human h2) {
this.h2 = h2;
}
public void h2talk() {
h2.talk("h2");
}
/**
* method inject(this method will execute automatic when instance creating)
*/
@Inject
public void h3talk(Human h3) {
h3.talk("h3");
}
@Inject
@FixedValue
private Human w;
public void wtalk() {
w.talk("w");
}
public void noise() {
this.h1talk();
this.h2talk();
this.h3talk(h1);
System.out.println(groupName);
this.wtalk();
}
}
MainTest类
import com.google.inject.Guice;
import com.google.inject.Injector;
public class MainTest {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new HumanModule());
Human h0 = injector.getInstance(Human.class);
h0.talk("h0");
HumanGroup group = injector.getInstance(HumanGroup.class);
for (int i = 0; i < 10; i++) {
injector.getInstance(HumanGroup.class);// test singleton
}
group.noise();
}
}
执行MainTest里的main方法打印结果为:
h0:WaWaWa~~(Cry)
1322056875399
h3:WaWaWa~~(Cry)
h1:WaWaWa~~(Cry)
h2:WaWaWa~~(Cry)
h3:WaWaWa~~(Cry)
*_*Childs*_*
w:I want a house.
附件可下载。