1.接口
package com.mycompany.addclient;
import com.google.inject.Singleton;
/**
* @author zhanyi
* @date 20181224 14:51:29
* @version V1.0
* @desc
*/
public interface Services {
public int mul();
public int Add();
}
2.实现类
package com.mycompany.addclient;
import com.google.inject.Inject;
/**
* @author Alexs
* @date 20181224 10:39:30
* @email alexs_zhan@powermaxtecher.net
* @version V1.0
* @desc
*/
public class GoodsService implements Services {
@Override
public int mul() {
System.out.println(this.getClass().getName() + "====mul()");
return 1;
}
@Override
public int Add() {;
System.out.println(this.getClass().getName() + "===Add()");
return 1;
}
}
package com.mycompany.addclient;
/**
* @author Alexs
* @date 20181224 10:39:20
* @email alexs_zhan@powermaxtecher.net
* @version V1.0
* @desc
*/
public class PeopleService implements Services{
@Override
public int mul() {
System.out.println(this.getClass().getName()+"====mul()");
return 0;
}
@Override
public int Add() {
System.out.println(this.getClass().getName()+"====Add()");
return 0;
}
}
3.元注解
package com.mycompany.addclient;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Alexs
* @date 20181224 17:04:23
* @Add http://www.baoquntecher.com
* @email alexs_zhan@powermaxtecher.net
* @version V1.0
* @desc
*/
@BindingAnnotation
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface People {
}
package com.mycompany.addclient;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Alexs
* @date 20181224 17:02:32
* @Add http://www.baoquntecher.com
* @email alexs_zhan@powermaxtecher.net
* @version V1.0
* @desc
*/
@BindingAnnotation
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Good {
}
package com.mycompany.addclient;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.name.Names;
import io.vertx.core.cli.annotations.Name;
/**
* @author Alexs
* @date 20181224 10:03:07
* @email alexs_zhan@powermaxtecher.net
* @version V1.0
* @desc
*/
public class AddModule implements Module{
@Override
public void configure(Binder binder) {
binder.bind(Services.class).annotatedWith(Good.class).to(GoodsService.class);
binder.bind(Services.class).annotatedWith(People.class).to(PeopleService.class);
binder.bind(Service.class).to(ServiceImpl.class);
}
}
package com.mycompany.addclient;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
/**
* @author Alexs
* @date 20181224 10:38:50
* @email alexs_zhan@powermaxtecher.net
* @version V1.0
* @desc
*/
public class ServiceImpl implements Service {
private Services people;
private Services goods;
/**
*
* @param people
* @param goods
*/
@Inject
public ServiceImpl(@People Services people, @Good Services goods) {
this.people = people;
this.goods = goods;
}
@Override
public int sum() {
people.Add();
goods.Add();
return 0;
}
}
4.main函数
package com.mycompany.addclient;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import io.vertx.core.Vertx;
/**
* @author Alexs
* @date 20181224 9:58:49
* @email alexs_zhan@powermaxtecher.net
* @version V1.0
* @desc
*/
public class AddClient {
Vertx vertx;
static ServiceImpl serviceImpl;
public static void main(String[] args) {
Injector injector = Guice.createInjector(new AddModule());
Service services = injector.getInstance(Service.class);
System.out.println("com.mycompany.addclient.AddClient.main()" + services.sum());
}
}
5.结果