GoogleGuice的使⽤⽅式
Google Guice(读⾳:juice)是从Google AdWords项⽬开源出来的⼀款轻量级的依赖注⼊(DI,Dependency Injection)框架(类似于spring boot依赖注⼊框架)
注解:@Inject 注⼊类(跟spring 的Autowired类似)
注解:@Singleton单例模式(跟spring 的Singleton类似)
主类:
package com.zhangmen;
import com.beust.jcommander.JCommander;
le.inject.Guice;
le.inject.Inject;
le.inject.Injector;机械英语翻译
import t.BasicModule;
import Student;
import com.zhangmen.rvice.TrackEventService;
import com.zhangmen.util.FileUtil;
import com.zhangmen.util.HdfsUtils;
slf4j.Slf4j;
import java.io.InputStream;
@Slf4j
怎么了英语
public class TrackDataMain {
@Inject
private HdfsUtils hdfsUtils;
@Inject
private TrackEventService trackEventService;
@Inject
citeprivate Student student;
public static void main(String[] args)throws Exception {
log.info("DeduplicationJob.args:{}", String.join(" ", args));
try{
// Injector injector = ateInjector(); //如果没有接⼝可以⽤这个,如果有接⼝及实现类,必须要在 BasicModule 中绑定 Injector injector = ateInjector(new BasicModule());
TrackDataMain main = Instance(TrackDataMain.class);
}catch(Throwable ex){
<(ex.getMessage(), ex);
throw ex;
}
}
private void execute(String[] args)throws Exception {webgame
InputStream in = ClassLoader().getResourceAsStream("./application.properties");
String[] loadConfigs = FileUtil.loadConfigs(in);
.addObject(new Object[]{student})
.acceptUnknownOptions(true)
.build()
.par(loadConfigs);
System.out.String());
System.out.Name());
System.out.println("输出语句");
String hdfsConfTmp = HdfsConfTmp();
System.out.println(hdfsConfTmp);
System.out.Name());
primeminister}
新起点英语一年级下册
}
绑定接⼝及实现类,让guice发现
le.inject.AbstractModule;
import com.zhangmen.rvice.TrackEventService;
import com.zhangmen.rvice.impl.TrackEventServiceImpl;
public class BasicModule extends AbstractModule {
@Override
protected void configure(){
bind(TrackEventService.class).to(TrackEventServiceImpl.class); // bind(SourceDelegation.class).to(KafkaSourceDelegation.class); }
}
接⼝及实现类
注意: default 关键字的⽤法
next是什么意思
package com.zhangmen.rvice;
/**
* @Author zsq
* @create 2021/6/17 17:35
* @Description:
*/
public interface TrackEventService {
/**
reader* default 修饰⽅法,则实现类的值优先于接⼝的⽅法
* @return
*/
default String getName(){
return null;
}
}
package com.zhangmen.rvice.impl;
import com.zhangmen.rvice.TrackEventService;
/**
* @Author zsq
* @create 2021/6/17 17:35
* @Description:
*/
public class TrackEventServiceImpl implements TrackEventService { @Override
public String getName(){
return"来玩耍呀";
fiata
}
}
实体类: 通过 jcommander 来注⼊配置参数
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
le.inject.Singleton;
import lombok.Data;
@Singleton
@Data
@Parameters(parators ="=")
public class Student {
@Parameter(names ="--sumer.rvers") private String name;
@Parameter(names ="--up.id") private String age;
}