How to access command line arguments in a spring configuration?

I have a Spring boot application that uses the Spring inbound channel adapter of the built-in integration procedure. I would like to pass a command line argument to a stored procedure. The Spring Boot doc says that SpringApplication converts any arguments to command-line options, from '- to a property and adding it to the Spring environment. What is the correct way to access this property in an 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>

The use ${arg1}here does not seem to be eliminated. What is the correct syntax, or do I need to define an additional property or property placeholder?

Launching an application, for example. s java -jar app.jar --arg1=5throws an exception

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}"

,

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

.

+4
2

Spring.

auto-configuration, PropertyPlaceholderConfigurer.

:

@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();
    }    
}

barrier: https://github.com/spring-projects/spring-integration-samples/tree/master/basic/barrier

+2

, XML JavaConfig. ,

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

Source: https://habr.com/ru/post/1624502/


All Articles