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.




附件可下载。

黑河是我国西北干旱区最重要的内陆河流之一,灌区分布及水利工程体系直接关系到流域农业发展、水资源配置与生态安全。 本资源包含黑河流域范围内的灌区空间分布矢量数据(Shapefile格式)与干支渠分布栅格图(TIF格式),可广泛应用于农业水资源管理、流域水文模拟、灌溉工程布局分析及生态水文研究等领域。 【数据内容】 灌区分布数据(Shapefile) 数据类型:矢量多边形(Polygon) 坐标系统:WGS 84 或 CGCS2000(具体可查看 .prj 文件)。 干支渠分布图(GeoTIFF) 数据类型:栅格图像(TIF) 分辨率:通常为10–30米,满足中尺度制图与分析; 图像内容:表示黑河流域干渠与支渠的空间路径分布,可作为水利网络基础图层; 内容描述:标识黑河流域主要灌区边界,包括各县(如张掖、高台、临泽、肃南等)所辖的骨干灌区、支渠灌区分布; 属性字段:灌区名称等; 应用价值:可用于构建灌溉水流路径、流量估算、水资源调度仿真模型等。 【典型应用场景】 流域灌溉调度研究:用于构建灌区供水模型,估算引水量与灌溉效率; 遥感与地理建模:与MODIS、Sentinel遥感数据叠加进行土地覆被分类或作物监测; 农业统计分析:与统计年鉴灌溉面积核对比对,服务于灌溉政策评估; 地图制图与展示:支持ArcGIS、QGIS、Mapbox等平台加载使用,可生成专题图; 水文模型输入:可作为SWAT、MIKE SHE 等模型的空间输入因子。 【附加说明】 文件命名清晰,包含 .shp, .shx, .dbf, .prj 等标准矢量格式; TIF 文件配有 .tfw 文件及标准色带,可直接叠加到DEM、水系图等背景图上; 可适配常用 GIS 软件(ArcGIS/QGIS)及建模工具; 数据来源规范,具有较高的空间精度与现势性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值