Java属性中的Mule属性占位符访问
在mule中部署到多个环境时,我有不同的属性文件。在我的src/main/resources中,我有Java属性中的Mule属性占位符访问,java,mule,Java,Mule,在mule中部署到多个环境时,我有不同的属性文件。在我的src/main/resources中,我有local.properties和test.properties文件。我还有一个全局属性占位符,我在mule app.properties中引用了该占位符,如仅根据我使用的服务器更改占位符环境变量中所述 例如,在local.properties文件中,我可以: username=John password=local username=Christi password=test 对于test.
local.properties
和test.properties
文件。我还有一个全局属性占位符,我在mule app.properties
中引用了该占位符,如仅根据我使用的服务器更改占位符环境变量中所述
例如,在local.properties
文件中,我可以:
username=John
password=local
username=Christi
password=test
对于test.properties
,我会:
username=John
password=local
username=Christi
password=test
在我的app-mule.properties中,我想指出:
mule.env=local or mule.env=test
所以实际上这很好用。但是当我必须在java类中访问这些属性时,比如Config.java
,它就不起作用了。我希望得到如下示例中所示的属性:
public class Config {
static Properties prop = new Properties();
static {
// load a properties file
try {
InputStream input = Config.class.getClassLoader().getResourceAsStream("mule-app.properties");
prop.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static final String USERNAME = prop.getProperty("username");
public static final String PASSWORD = prop.getProperty("password");
}}
如果我直接在mule app.properties
文件中定义所有属性,而不是引用特定的属性文件,那么这个java类工作得很好。所以我的问题是,如何让java代码访问本地和测试属性文件中定义的属性,只需访问mule app.properties
中的引用
编辑:
我的有效解决方案,由@bigdestroyer建议:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Config {
static Properties prop = new Properties();
static {
// load a properties file
try {
InputStream input = Config.class.getClassLoader().getResourceAsStream("mule-app.properties");
prop.load(input);
String type = prop.getProperty("mule.env");
input = Config.class.getClassLoader().getResourceAsStream(type + ".properties");
prop.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static final String USERNAME = prop.getProperty("username");
public static final String PASSWORD = prop.getProperty("password");
}}
如果我没有误解你,你可以这样做:
public class Config {
static Properties prop = new Properties();
static {
// load a properties file
try {
InputStream input = Config.class.getClassLoader().getResourceAsStream("mule-app.properties");
InputStream input =
prop.load(input);
String type = prop.getProperty("mule.env"); //<-- here you get local or test
input = getClass().getClassLoader().getResourceAsStream(type + ".properties"); // here you get the file
prop.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static final String USERNAME = prop.getProperty("username");
public static final String PASSWORD = prop.getProperty("password");
}}
公共类配置{
静态属性prop=新属性();
静止的{
//加载属性文件
试一试{
InputStream input=Config.class.getClassLoader().getResourceAsStream(“mule app.properties”);
InputStream输入=
道具载荷(输入);
字符串类型=prop.getProperty(“mule.env”)//