Java 基于命令行参数的注入实现

Java 基于命令行参数的注入实现,java,spring-boot,command-line,interface,autowired,Java,Spring Boot,Command Line,Interface,Autowired,有没有办法在Spring Boot中基于命令行参数注入特定的接口实现 我有一个数据加载应用程序,根据命令行参数,我需要加载特定类型的数据 这是我的主类和CommandLineRunner: @SpringBootApplication public class DataLoadersApplication implements CommandLineRunner { private Type1LoadProcess type1LoadProcess; private Type2

有没有办法在Spring Boot中基于命令行参数注入特定的接口实现

我有一个数据加载应用程序,根据命令行参数,我需要加载特定类型的数据

这是我的主类和CommandLineRunner:

@SpringBootApplication
public class DataLoadersApplication implements CommandLineRunner {

    private Type1LoadProcess type1LoadProcess;
    private Type2LoadProcess type2LoadProcess;

    public DataLoadersApplication(Type1LoadProcess type1LoadProcess,
      Type2LoadProcess type2LoadProcess) {
        this.type1LoadProcess = type1LoadProcess;
        this.type2LoadProcess = type2LoadProcess;
    }

    public static void main(String[] args) {
        SpringApplication.run(DataLoadersApplication.class, args);
    }

    @Override
    public void run(String... args) {
        if (args[0].equalsIgnoreCase("load-type1")) {
            type1LoadProcess.process();
        } else if (args[0].equalsIgnoreCase("load-type2")) {
            type2LoadProcess.process();
        } 
    }
}
有没有一种方法可以让我创建一个带有两个实现的
DataLoadeProcess
接口
Type1DataLoadProcess
Type2DataLoadProcess
,并基于命令行参数将实现注入到主类中?

我将用于此。只需将您的实现转换为Springbean,然后基于活动概要文件加载所需的Bean

然后,当您在启动应用程序时将活动配置文件指定为命令行参数时,应使用相应的Bean。

我将使用它。只需将您的实现转换为Springbean,然后基于活动概要文件加载所需的Bean


然后,当您在启动应用程序时将活动配置文件指定为命令行参数时,应使用相应的Bean。

您可以使用Spring配置文件来实现您的目标:

创建接口DataLoadProcess

然后是课程:

@Component
@Profile("type1")
public class Type1LoadProcess implements DataLoadProcess {
}

@Component
@Profile("type2")
public class Type2LoadProcess implements DataLoadProcess {
}
然后可以注入接口类型,如下所示:

@Autowired 
DataLoadProcess dataLoadProcessor;
现在,您可以使用其中一个配置文件启动应用程序,例如,使用系统属性集:

-Dspring.profiles.active=type1

您可以使用Spring配置文件来实现您的目标:

创建接口DataLoadProcess

然后是课程:

@Component
@Profile("type1")
public class Type1LoadProcess implements DataLoadProcess {
}

@Component
@Profile("type2")
public class Type2LoadProcess implements DataLoadProcess {
}
然后可以注入接口类型,如下所示:

@Autowired 
DataLoadProcess dataLoadProcessor;
现在,您可以使用其中一个配置文件启动应用程序,例如,使用系统属性集:

-Dspring.profiles.active=type1

一个完整的例子是

@SpringBootApplication
public class DataLoadersApplication implements CommandLineRunner {
    public interface LoadProcess {
        void doLoad();
    }

    @Component // default that exists unconditionally in any profile
    static class Type1LoadProcess implements LoadProcess {
        @Override public void doLoad() { System.out.println("Load1"); }
    }

    @Profile("type2") // this only exists in the type2 profile
    @Primary          // if it exists it gets picked over others
    @Component
    static class Type2LoadProcess implements LoadProcess {
        @Override public void doLoad() { System.out.println("Load2"); }
    }
    // need a 3rd? @Profile("type3") @Primary @Component

    @Autowired  // need one of them here
    private LoadProcess loadProcess;

    @Override
    public void run(String... args) {
        loadProcess.doLoad();
    }

    public static void main(String[] args) {
        SpringApplication.run(DataLoadersApplication.class, args);
    }
}
这利用了概要文件,并使用主bean机制来允许在未指定概要文件时使用默认实现

然后,您可以通过中列出的任何选项选择使用哪个配置文件,例如通过设置环境变量

SPRING_PROFILES_ACTIVE=type2 java -jar myApp.jar
使用属性

java -Dspring.profiles.active=type2 java -jar myApp.jar
甚至是一个参数

java -jar myApp.jar --spring.profiles.active=type2

当您需要type2实现时。您仍然可以将“type1”作为活动配置文件,即使它没有定义。它仍然会做正确的事情并使用type1代码,因为这是默认的

@SpringBootApplication
public class DataLoadersApplication implements CommandLineRunner {
    public interface LoadProcess {
        void doLoad();
    }

    @Component // default that exists unconditionally in any profile
    static class Type1LoadProcess implements LoadProcess {
        @Override public void doLoad() { System.out.println("Load1"); }
    }

    @Profile("type2") // this only exists in the type2 profile
    @Primary          // if it exists it gets picked over others
    @Component
    static class Type2LoadProcess implements LoadProcess {
        @Override public void doLoad() { System.out.println("Load2"); }
    }
    // need a 3rd? @Profile("type3") @Primary @Component

    @Autowired  // need one of them here
    private LoadProcess loadProcess;

    @Override
    public void run(String... args) {
        loadProcess.doLoad();
    }

    public static void main(String[] args) {
        SpringApplication.run(DataLoadersApplication.class, args);
    }
}
这利用了概要文件,并使用主bean机制来允许在未指定概要文件时使用默认实现

然后,您可以通过中列出的任何选项选择使用哪个配置文件,例如通过设置环境变量

SPRING_PROFILES_ACTIVE=type2 java -jar myApp.jar
使用属性

java -Dspring.profiles.active=type2 java -jar myApp.jar
甚至是一个参数

java -jar myApp.jar --spring.profiles.active=type2

当您需要type2实现时。您仍然可以将“type1”作为活动配置文件,即使它没有定义。它仍然会做正确的事情,并使用type1代码,因为这是默认值。

。。。然后需要
@injectdataloadprocess dataLoadProcessor
?是的。我把这个加在我的答案上。。。然后需要
@injectdataloadprocess dataLoadProcessor
?是的。我将此添加到我的答案中