java组件中的Mule属性
在我的Mule项目中,我有一个属性文件,其中包含我的http地址 服务器地址=http://localhost:8080/test/ 然后在我的流中,我将引用它为${server.address},但是如何在java组件类中引用该属性呢 例如:java组件中的Mule属性,java,properties,mule,Java,Properties,Mule,在我的Mule项目中,我有一个属性文件,其中包含我的http地址 服务器地址=http://localhost:8080/test/ 然后在我的流中,我将引用它为${server.address},但是如何在java组件类中引用该属性呢 例如: public String address = ${server.address} 您可以将java类中的属性设置为Spring属性。对于单例对象,属性将在流启动时设置 <flow name="propertyprojectFlow1" do
public String address = ${server.address}
您可以将java类中的属性设置为Spring属性。对于单例对象,属性将在流启动时设置
<flow name="propertyprojectFlow1" doc:name="propertyprojectFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8581" path="echoServer" doc:name="HTTP"/>
<component doc:name="Java">
<singleton-object class="MyClass">
<property key="server" value="${server.address}"/>
</singleton-object>
</component>
</flow>
您可以将java类中的属性设置为Spring属性。对于单例对象,属性将在流启动时设置
<flow name="propertyprojectFlow1" doc:name="propertyprojectFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8581" path="echoServer" doc:name="HTTP"/>
<component doc:name="Java">
<singleton-object class="MyClass">
<property key="server" value="${server.address}"/>
</singleton-object>
</component>
</flow>
可以加载该属性或任何其他属性,也可以加载属性文件。尽管它可能会产生一些性能问题,但每次执行流时都会加载属性文件
public Properties loadProperties(String name) {
if(properties == null){
return loadProperties(name, Thread.currentThread().getContextClassLoader());
} else {
return properties;
}
}
然后:
Properties props = loadProperties("application.properties");
String url = props.getProperty("server.address");
对@Matt answer进行一些更改
import org.apache.log4j.Logger;
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
public class MyClass implements Callable {
private static Logger logger = Logger.getLogger(MyClass.class);
private String server;
private Properties properties;
public MyClass() { }
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
properties = loadProperties("application.properties");
server = properties.getProperty("server.address");
logger.info("Server is " + server);
return null;
}
public Properties loadProperties(String name) {
if(properties == null){
return loadProperties(name, Thread.currentThread().getContextClassLoader());
} else {
return properties;
}
}
}
可以加载该属性或任何其他属性,也可以加载属性文件。尽管它可能会产生一些性能问题,但每次执行流时都会加载属性文件
public Properties loadProperties(String name) {
if(properties == null){
return loadProperties(name, Thread.currentThread().getContextClassLoader());
} else {
return properties;
}
}
然后:
Properties props = loadProperties("application.properties");
String url = props.getProperty("server.address");
对@Matt answer进行一些更改
import org.apache.log4j.Logger;
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
public class MyClass implements Callable {
private static Logger logger = Logger.getLogger(MyClass.class);
private String server;
private Properties properties;
public MyClass() { }
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
properties = loadProperties("application.properties");
server = properties.getProperty("server.address");
logger.info("Server is " + server);
return null;
}
public Properties loadProperties(String name) {
if(properties == null){
return loadProperties(name, Thread.currentThread().getContextClassLoader());
} else {
return properties;
}
}
}
属性文件中的属性名称:
your.property.name.in.properties.file=testing
Java组件:
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
import org.springframework.beans.factory.annotation.Value;
public class PropertyTest implements Callable {
@Value("${your.property.name.in.properties.file}")
private String yourPropertyNameInPropertiesFile;
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
System.out.println("yourPropertyNameInPropertiesFile"+yourPropertyNameInPropertiesFile);
// TODO Auto-generated method stub
return null;
}
}
属性文件中的属性名称:
your.property.name.in.properties.file=testing
Java组件:
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
import org.springframework.beans.factory.annotation.Value;
public class PropertyTest implements Callable {
@Value("${your.property.name.in.properties.file}")
private String yourPropertyNameInPropertiesFile;
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
System.out.println("yourPropertyNameInPropertiesFile"+yourPropertyNameInPropertiesFile);
// TODO Auto-generated method stub
return null;
}
}
如果要为变量设置正确的键值?例如:但是类将其读取为字符串[flowVars['FileName']],而不是varaible的值如果我想为变量设置正确的键值?例如:但是类将其读取为字符串[flowVars['FileName']],而不是变量的值