简体   繁体   中英

How to access command line arguments in Spring configuration?

I have a Spring Boot application that uses a Spring Integration Stored Procedure Inbound Channel Adapter. I would like to pass a command line argument to the stored procedure. The Spring Boot doc says that SpringApplication will convert any command line option arguments starting with '--' to a property and add it to the Spring Environment. What is the correct way to access this property in the int-jdbc:parameter element?

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new SpringApplication("integration.xml").run(args);
        System.out.println("Hit Enter to terminate");
        System.in.read();
        ctx.close();
    }    
}

integration.xml

<int-jdbc:stored-proc-inbound-channel-adapter
    stored-procedure-name="sp_get_some_records"
    data-source="dataSource"
    channel="recs.in.channel"
    id="recs.in">

    <int:poller fixed-rate="86400" time-unit="SECONDS" />

    <int-jdbc:parameter name="myarg" type="java.lang.Integer" value="${arg1}" />

</int-jdbc:stored-proc-inbound-channel-adapter>

Using ${arg1} here doesn't seem to get resolved. What is the proper syntax, or do I need to define an additional property or property placeholder?

Starting the app, eg with java -jar app.jar --arg1=5 throws an exception of

Error converting typed String value for bean property 'value'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type [java.lang.String] to required type [java.lang.Integer]; nested exception is java.lang.NumberFormatException: For input string: "${arg1}"

I thought first it had to do with type conversion and tried with

<int-jdbc:parameter name="myarg" type="java.lang.Integer" value="#{ T(java.lang.Integer).parseInt(arg1) }" />

but that didn't work either.

You don't have any Spring Boot stuff.

I mean auto-configuration , which includes PropertyPlaceholderConfigurer , too.

That must be something like this:

@SpringBootApplication
@ImportResource("integration.xml")
public class Application {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
        System.out.println("Hit Enter to terminate");
        System.in.read();
        ctx.close();
    }    
}

I've just checked with our barrier sample: https://github.com/spring-projects/spring-integration-samples/tree/master/basic/barrier

If I were you, I would convert your XML config to JavaConfig. Then you could just access your property in your config class using

@Value("${arg1}")
private Integer arg1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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