Guice使用練習

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.




附件可下載。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章