Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对象工厂的依赖注入_Java_Dependency Injection_Dagger 2 - Fatal编程技术网

Java 对象工厂的依赖注入

Java 对象工厂的依赖注入,java,dependency-injection,dagger-2,Java,Dependency Injection,Dagger 2,如果我们有几个类,则如下所示: @Device() @Model("some model") @Variant("A") public class SomeModelVariantA extends BaseDevice { public SomeModelVariantA (InputStream in, OutputStream out, int encoding) throws IOException { super(in, out, encoding);

如果我们有几个类,则如下所示:

@Device()
@Model("some model")
@Variant("A")
public class SomeModelVariantA extends BaseDevice {

    public SomeModelVariantA (InputStream in, OutputStream out, int encoding) throws IOException {
        super(in, out, encoding);
    }

    @Override
    @CommandSpec("41")
    @WithoutArguments
    @WithoutResponse
    public Response init(Command args) throws NotSupportedException, IOException {
        return null;
    }
/**
 * Generated on Fri Aug 28 11:03:21 EEST 2015
 */
public final class SomeModelVariantA extends BaseDevice {
  public SomeModelVariantA (InputStream arg0, OutputStream arg1, int arg2) throws IOException {
    super(arg0, arg1, arg2);
  }

  @Override
  public Response init(Command arg0) throws NotSupportedException, IOException {
    StringBuilder builder = new StringBuilder("");
    Response response = new Response();

    customCommand(41, builder.toString());
    return response;
  }
我使用JavaPoet和注释处理生成类实现。 我有我使用的所有注释的项目,项目编译器有注释处理器来处理注释类,应用程序项目有注释、生成的类和其他逻辑。所有生成的类都扩展了base
BaseDevice
类,看起来像:

@Device()
@Model("some model")
@Variant("A")
public class SomeModelVariantA extends BaseDevice {

    public SomeModelVariantA (InputStream in, OutputStream out, int encoding) throws IOException {
        super(in, out, encoding);
    }

    @Override
    @CommandSpec("41")
    @WithoutArguments
    @WithoutResponse
    public Response init(Command args) throws NotSupportedException, IOException {
        return null;
    }
/**
 * Generated on Fri Aug 28 11:03:21 EEST 2015
 */
public final class SomeModelVariantA extends BaseDevice {
  public SomeModelVariantA (InputStream arg0, OutputStream arg1, int arg2) throws IOException {
    super(arg0, arg1, arg2);
  }

  @Override
  public Response init(Command arg0) throws NotSupportedException, IOException {
    StringBuilder builder = new StringBuilder("");
    Response response = new Response();

    customCommand(41, builder.toString());
    return response;
  }
我已经生成了几个具体的设备,下一步是创建一个使用这些设备的api

public class Api {

    BaseDevice device;

    Api(String device, InputStream is, OutputStream os, int encoding) {
        if ("SomeModelVariandA1".contentEquals(device)) {
            device = new SomeModelVariantA(is, os, encoding);
        }
        ... else if for other devices
    }

    public void callInit() {
        device.init(null);
    }
}
那么,我如何为所有设备生成工厂,并通过一些字符串名称将设备注入Api类中呢。如何处理提供使用api构造函数的输入和输出流创建的设备。dagger 2和google auto factory是否适合此用途,是否有人可以提供一个使用示例


谢谢

您需要设置为
concreteClass
完整类名(包+类名),然后使用
class.forName(concreteClass)

例如,如果在
com.blabla.classes
包中定义了
ClassA
,则需要设置为
concreteClass
“com.blablabla.classes.ClassA”
。 然后,如果您只想创建扩展
BaseClass
的类对象,则需要定义如下所示的新类:

Class injectedClass=Class.forName(concreteClass)
或者,如果要创建所有类对象,则需要定义:


Class injectedClass=Class.forName(concreteClass)

我试图生成工厂,但无法使用
Class.forName(concreteClass)
解析这些具体类。生成逻辑在编译器项目中,生成的类在其他项目中。您能提供代码示例吗?我无法理解
编译器项目
。如果你的意思是,它们在不同的jar中,那么,你需要将这个jar包含在你的类路径中。我在应用程序项目中有注释类,并使用编译器项目在应用程序项目中使用注释处理器生成这些具体类。