如何在Java代码中从属性文件访问属性

如何在Java代码中从属性文件访问属性

本文介绍了如何在Java代码中从属性文件访问属性(Mule ESB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从用m子编写的Java代码的属性文件中获取属性.

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是存储在属性文件中的属性

prop1 is a property stored in properties file

prop1 = " 001 | key1 cannot be blank"

我希望将prop1解析为001 | key1 cannot be blank. ${propname}不起作用.我无法使用值注释,因为我想将属性的值保存在消息中.

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.

推荐答案

最好的方法是不依赖于任何Mule特定代码,并且像对待其他任何参数一样处理该属性,只需将其作为参数传递即可.因此,当实例化或调用某个方法时,只需在Mule端传递属性即可:

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>

否则,您将取决于Java代码的加载方式,因为您需要注入 ConfigurationProperties 实例,并使用它来解析您的属性:

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

这意味着您的Java代码必须是SDK模块的一部分,以便进行注入.

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

这篇关于如何在Java代码中从属性文件访问属性(Mule ESB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:04