简体   繁体   English

如何将命令行 arguments 传递给焊接容器?

[英]How to pass command line arguments to a weld container?

I have a application which is started via command line like java -jar MyAssembledJarWithAllDep.jar -foo bar我有一个通过命令行启动的应用程序,例如java -jar MyAssembledJarWithAllDep.jar -foo bar

I am using weld.se to be able to use the jakarta ee cdi specification.我使用weld.se 能够使用jakarta ee cdi 规范。 Furthermore I am using the apache cli tool to parse the comman line arguments.此外,我正在使用 apache cli 工具来解析命令行 arguments。

Here are my maven imports:这是我的 maven 进口:

        <dependency>
            <groupId>jakarta.enterprise</groupId>
            <artifactId>jakarta.enterprise.cdi-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se</artifactId>
            <version>2.4.6.Final</version>
        </dependency>

        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.4</version>
        </dependency>

That's how I initialize the container:这就是我初始化容器的方式:

    public static void main(String[] args) {
        Weld weld = new Weld();
        WeldContainer container = weld.initialize();
        container.select(MyRunnerClass.class).get().run(args);
        container.shutdown();
    }

I could do the following:我可以执行以下操作:

public void run(String[] args) {
   CommandLineParser clp = new CommandLineParser(args);
   clp.parse();

But since I want to use the full support of cdi, I cannot the object since I created it myself!但是由于我想使用cdi的全面支持,所以我无法使用object,因为我自己创建了它! So how do I pass the arguments to the container so that weld can create the CommandLineParser himself with the needed arguments?那么如何将 arguments 传递给容器,以便 Weld 可以使用所需的 arguments 自己创建CommandLineParser

I am not familiar with apache cli tools so I am assuming CommandLineParser is not a bean on its own and you want to turn it into a bean.我不熟悉 apache cli 工具,所以我假设CommandLineParser本身不是一个 bean,你想把它变成一个 bean。 In that case you need to provide a producer method which can make use of any local metadata in creation of the object.在这种情况下,您需要提供一个生产者方法,该方法可以在创建 object 时使用任何本地元数据。

The following method is a producer method and needs to be placed in another bean class for it to work (see CDI specification for more details):以下方法是生产者方法,需要放在另一个 bean class 中才能工作(有关详细信息,请参阅 CDI 规范):

@Produces
@ApplicationScoped // or any other scope that you want to use for the bean!
public CommandLineParser produceCmdLineParser() {
  String[] args = retrieveArgs(); // grab arguments from your main class or other place
  return new CommandLineParser(args);
}

The above producer method will then be invoked by Weld (do not invoke it manually) when you ask the CDI container for bean of type CommandLineParser via standard or dynamic injection.当您通过标准或动态注入向 CDI 容器请求CommandLineParser类型的 bean 时,Weld 将调用上述生产者方法(不要手动调用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM