Guice:轻量级IOC/DI 容器

Guice 简介

Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。Guice是类型安全的,它能够对构造函数,属性,方法(包含任意个参数的任意方法,而不仅仅是setter方法)进行注入。Guice采用Java加注解的方式进行托管对象的配置,充分利用IDE编译器的类型安全检查功能和自动重构功能,使得配置的更改也是类型安全的。Guice提供模块对应的抽象module,使得架构和设计的模块概念产物与代码中的module类一一对应,更加便利的组织和梳理模块依赖关系,利于整体应用内部的依赖关系维护,而其他IOC框架是没有对应物的。此外,借助privateModule的功能,可以实现模块接口的明确导出和实现封装,使得支持多数据源这类需求实现起来异常简单。

示例代码

package com.conquer.comutils.core.guice.business;

public interface HelloWorld {
    String sayHello();
}
package com.conquer.comutils.core.guice.business;

import javax.inject.Singleton;

@Singleton// 被 di 容器 只实例化 一次
public class HelloWorldImpl implements HelloWorld {
    public HelloWorldImpl() {
        System.out.println("HelloWorldImpl constructs");
    }

    @Override
    public String sayHello() {
        return "Hello, world!";
    }
}
package com.conquer.comutils.core.guice.business;


import javax.inject.Inject;// 使用 java 规范的 inject 注解,避免依赖 guice
import javax.inject.Named;

public class MyBusiness {

    @Inject
//    @Named("driver")// 多个实例的注入选择,也可以通过 继承 Qualifier实现不同的分类器 来实现区分
    private HelloWorld helloWorld;

//    @Inject
//    public HelloResource(HelloWorld helloWorld) {
//        this.helloWorld = helloWorld;
//    }

    public String test() {
        return helloWorld.sayHello();
    }
}
以上三个类是我们的业务代码,可以看到他们没有任何的 guice 代码侵入,仅仅是依赖了一些 javax inject 规范而已
Guice 的 Module,架构设计中的模块的对应物,起到外置配置类结构关系的作用
package com.conquer.comutils.core.guice;

import com.conquer.comutils.core.guice.business.HelloWorld;
import com.conquer.comutils.core.guice.business.HelloWorldImpl;
import com.conquer.comutils.core.guice.business.MyBusiness;
import com.google.inject.Binder;
import com.google.inject.Module;

// 在自定义的Module类中进行绑定
public class MyGuiceModule implements Module {
    public void configure(Binder binder) {
        binder.bind(MyBusiness.class);
        binder.bind(HelloWorld.class).to(HelloWorldImpl.class)
        //                使用 javax.inject.Singleton() 标注 实现类 实现单例
//                .in(com.google.inject.Scopes.SINGLETON)// 这里设置单例,默认不是单例的而是每次创建
        ;

//        try {
//        默认使用无参构造方法创建实例,也可以通过这种方式指定创建时候使用的构造方法
//            binder.bind(HelloWorld.class).toConstructor(HelloWorldImpl.class.getConstructor());
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        }
    }
}
测试类:
package com.conquer.comutils.core.guice;

import com.conquer.comutils.core.guice.business.HelloWorld;
import com.conquer.comutils.core.guice.business.HelloWorldImpl;
import com.conquer.comutils.core.guice.business.MyBusiness;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

public class TestGuice {
    public static void main(String[] args) {
        Injector inj = Guice.createInjector(new Module() {
            @Override
            public void configure(Binder binder) {
                binder.bind(HelloWorld.class).to(HelloWorldImpl.class)
//                使用 javax.inject.Singleton() 标注 实现类 实现单例
//                        .in(com.google.inject.Scopes.SINGLETON)// 这里设置单例,默认不是单例的而是每次创建
                ;
            }
        });
        for (int i = 0; i < 2; i++) {
            HelloWorld hw = inj.getInstance(HelloWorld.class);
            System.out.println(hw);// 设置 com.google.inject.Scopes.SINGLETON 这里打印同一个对象
            System.out.println(hw.sayHello());
        }

        System.out.println("==========================");
        
        // test business
        Injector injector = Guice.createInjector(new MyGuiceModule());
        MyBusiness helloResource = injector.getInstance(MyBusiness.class);
        String test = helloResource.test();
        System.out.println("test: " + test);
    }
}
输出:
HelloWorldImpl constructs
com.conquer.comutils.core.guice.business.HelloWorldImpl@3b58fa97
Hello, world!
com.conquer.comutils.core.guice.business.HelloWorldImpl@3b58fa97
Hello, world!
==========================
HelloWorldImpl constructs
test: Hello, world!







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值