How to read @Value property outside the class that implements the Callable interface at MuleESB? In my case the some_property:
How can I get this value in the foo() method? I don't want to pass it as a parameter.
public class MainClass implements Callable {
@Value("${some_property}")
String some_property;
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
System.out.println(some_property); //some_property is ok
ClassB obB = new ClassB();
obB.foo();// some_property inside this method is null
}
}
public ClassB {
@Value("${some_property}")
String some_property;
public void foo() {
System.out.println(some_property);
}
}
EDIT: some more information about my problem
My MuleESB project contains a resource file located in the following file:
/MY_PROJECT/src/main/resources/services.properties
It contains some values like
some_property = AAAA
another_property = BBBB
This file is refferenced in one on my flow.xml files:
/MY_PROJECT/src/main/app/common.xml
<context:property-placeholder location="classpath:services.properties"
order="4" ignore-unresolvable="true" />
I want to use this config in another flow. I have Java component there an a class that implemments the Callable interface
public class MainClass implements Callable {
@Value("${some_property}")
String some_property;
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
System.out.println(some_property); //some_property is ok
ClassB obB = new ClassB();
obB.foo();// some_property inside this method is null
}
}
I have second class ClassB that will be often used. I want to run use it within previously mentioned onCall() method from MainClass
public ClassB {
@Value("${some_property}")
String some_property;
public void foo() {
System.out.println(some_property);
}
}
I want to load some_property value from my services.properties file. I use the following code
@Value("${some_property}")
String some_property;
I want to read this value only in ClassB foo() method. But there is null now (instead of MainClass onCall() method where the value is set). ClassB is independent and may be used in several onCall() methods so I don't want to read this properties in the MainClass and pass them to ClassB constructor or methods. How can I achive this?
I'd just use a constructor to read the props file. If you are creating many ClassB objects you may want to change this read the file once, rather than on each new instance.
public ClassB {
String some_property;
public ClassB() {
Properties p = new Properties();
try {
p.load( this.getClass().getResourceAsStream("your-props-file.properties") );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
some_property = p.getProperty("some_property");
}
public void foo() {
System.out.println(some_property);
}
}