Guice是由Google大牛Bob lee开发的一款轻量级的java IoC容器。其优势在于:
  速度快,号称比spring快100倍。
  无外部配置(如需要使用外部可以可以选用Guice的扩展包),完全基于annotation特性,支持重构,代码静态检查。
  简单,快速,基本没有学习成本。
  Guice和spring各有所长,Guice更适合与嵌入式或者高性能但项目简单方案,如OSGI容器,spring更适合大型项目组织。
  注入方式
  在我们谈到IOC框架,首先我们的话题将是构造,属性以及函数注入方式,Guice的实现只需要在构造函数,字段,或者注入函数上标注@Inject,如:
构造注入
public class OrderServiceImpl implements OrderService {
private ItemService itemService;
private PriceService priceService;
@Inject
public OrderServiceImpl(ItemService itemService, PriceService priceService) {
this.itemService = itemService;
this.priceService = priceService;
}
...
}
属性注入
public class OrderServiceImpl implements OrderService {
private ItemService itemService;
private PriceService priceService;
@Inject
public void init(ItemService itemService, PriceService priceService) {
this.itemService = itemService;
this.priceService = priceService;
}
...
}
函数(setter)注入
public class OrderServiceImpl implements OrderService {
private ItemService itemService;
private PriceService priceService;
@Inject
public void setItemService(ItemService itemService) {
this.itemService = itemService;
}
@Inject
public void setPriceService(PriceService priceService) {
this.priceService = priceService;
}
...
}