Home » Java » Spring Boot properties usage in Apache Camel route

Spring Boot properties usage in Apache Camel route

Posted by: admin December 28, 2021 Leave a comment

Questions:

Is this possible to use Spring Boot properties in Apache Camel route? @Value is working fine but is this possible to directly place in place holders of expressions.

Update: I know PropertiesComponent but which will be one more configuration apart from Applicaiton.yml that I don’t like to have.

application.yml

sftp:
  host:     10.10.128.128
  user:     ftpuser1
  password: ftpuser1password  
  path:     /tmp/inputfile/test1

Spring Boot Apache Camel Route:

 @Value("${sftp.user}")
    private String sftpUser;

    @Value("${sftp.host}")
    private String sftpHost;

    @Value("${sftp.password}")
    private String sftpPassword;

    @Value("${sftp.path}")
    private String sftpInPath;

    from("sftp://"+sftpUser+"@"+sftpHost+sftpInPath+"?delete=true&password="+sftpPassword)
//this is working

    from("sftp://${sftp.user}@${sftp.host}${sftp.path}?password=${sftp.password}")
// is this possible something like this?
Answers:

You can use property placeholders (http://camel.apache.org/properties.html) in Camel like this:

from("sftp://{{sftp.user}}@{{sftp.host}}{{sftp.path}}?password={{sftp.password}}")

###

Instead of injecting all you properties to separate fields you can inject your full link like that:

@Value("#{'sftp://'+'${sftp.user}'+'@'+'${sftp.host}'+'${sftp.path}'+'?delete=true&password='+'${sftp.password}'}")
private String fullLink;

Then, you can use it in from method.

###

Haven’t tried it locally but you could try to use this, by adding this properties component into the camel context( maybe u need to override the camel context configuration). Then you could use {{file.uri}} inside the from parts.

PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("classpath:com/mycompany/myprop.properties");
context.addComponent("properties", pc);