How to access property from property file in java code( Mule ESB)

依然范特西╮ 提交于 2020-02-04 05:28:07

问题


I am trying to fetch a property from the properties file in java code written in mule.

Class Example {
@NotBlank("message" = "${prop1}")
String key1;
String key2;
}

prop1 is a property stored in properties file

prop1 = " 001 | key1 cannot be blank"

I want prop1 to be resolved as 001 | key1 cannot be blank. ${propname} doesn't work. I can't use the value annotation as I want to save the value of the property in the message.


回答1:


The best approach for this is to not depend on any Mule specific code and deal with the property as you would any other argument, just passing it as a parameter. So when instantiating or calling a certain method, you'd just pass in the property at the Mule side:

<java:new class="com.me.Person" constructor="Person(String, Integer)">
    <java:args>#[{
      name: Mule::p('prop1'),
      age: 30
    }]</java:args>
</java:new>

Otherwise you'll depend on how that Java code is loaded since you'll need to inject a ConfigurationProperties instance and use it to resolve your property:

  @Inject
  private ConfigurationProperties configurationProperties;

  String getProperty(String name) {
     return configurationProperties.resolveStringProperty(name).orElse(null);
  }

This means your Java code has to be part of an SDK module so the injection takes place.



来源:https://stackoverflow.com/questions/59821078/how-to-access-property-from-property-file-in-java-code-mule-esb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!