Google Guice的使用方式

Google Guice(读音:juice)是从Google AdWords项目开源出来的一款轻量级的依赖注入(DI,Dependency Injection)框架(类似于spring boot依赖注入框架)

注解:@Inject 注入类(跟spring 的Autowired类似)
注解:@Singleton单例模式(跟spring 的Singleton类似)

主类:

package com.zhangmen;

import com.beust.jcommander.JCommander;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.zhangmen.context.BasicModule;
import com.zhangmen.entry.Student;
import com.zhangmen.service.TrackEventService;
import com.zhangmen.util.FileUtil;
import com.zhangmen.util.HdfsUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.InputStream;

@Slf4j
public class TrackDataMain {

    @Inject
    private HdfsUtils hdfsUtils;

    @Inject
    private TrackEventService trackEventService;

    @Inject
    private Student student;

    public static void main(String[] args) throws Exception {
        log.info("DeduplicationJob.args:{}", String.join(" ", args));
        try {
//            Injector injector = Guice.createInjector(); //如果没有接口可以用这个,如果有接口及实现类,必须要在 BasicModule 中绑定
            Injector injector = Guice.createInjector(new BasicModule());
            TrackDataMain main = injector.getInstance(TrackDataMain.class);
            main.execute(args);
        } catch (Throwable ex) {
            log.error(ex.getMessage(), ex);
            throw ex;
        }
    }
    private void execute(String[] args) throws Exception {
        InputStream in = TrackDataMain.class.getClassLoader().getResourceAsStream("./application.properties");
        String[] loadConfigs = FileUtil.loadConfigs(in);
        JCommander.newBuilder()
                .addObject(new Object[]{student})
                .acceptUnknownOptions(true)
                .build()
                .parse(loadConfigs);
        System.out.println(student.toString());
        System.out.println(student.getName());

        System.out.println("输出语句!!!");
        String hdfsConfTmp = hdfsUtils.getHdfsConfTmp();
        System.out.println(hdfsConfTmp);
        System.out.println(trackEventService.getName());
    }
}

绑定接口及实现类,让guice发现

package com.zhangmen.context;

import com.google.inject.AbstractModule;
import com.zhangmen.service.TrackEventService;
import com.zhangmen.service.impl.TrackEventServiceImpl;
public class BasicModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(TrackEventService.class).to(TrackEventServiceImpl.class);
//        bind(SourceDelegation.class).to(KafkaSourceDelegation.class);
    }
}

接口及实现类
注意: default 关键字的用法

package com.zhangmen.service;

/**
 * @Author zsq
 * @create 2021/6/17 17:35
 * @Description:
 */
public interface TrackEventService {

    /**
     * default 修饰方法,则实现类的值优先于接口的方法
     * @return
     */
    default String getName() {
        return null;
    }
}

package com.zhangmen.service.impl;

import com.zhangmen.service.TrackEventService;

/**
 * @Author zsq
 * @create 2021/6/17 17:35
 * @Description:
 */
public class TrackEventServiceImpl implements TrackEventService {
    @Override
    public String getName() {
        return "来玩耍呀";
    }
}

实体类: 通过 jcommander 来注入配置参数

package com.zhangmen.entry;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.inject.Singleton;
import lombok.Data;

@Singleton
@Data
@Parameters(separators = "=")
public class Student {

    @Parameter(names = "--kafka.consumer.servers")
    private String name;

    @Parameter(names = "--kafka.consumer.group.id")
    private String age;
}

上一篇:PyQt5基础学习-QtPrintSupport.QPrinter(打印机) 1.painter.begin(将绘图重定向到打印机) 2.QTextEdit().grab(获得文本编辑框的界面) 3.


下一篇:mongodb-4.4.10版本的索引管理:创建、删除