Mule ESB 在 Callable class 之外读取 @Value 属性
Mule ESB read @Value property outside Callable class
如何读取 @Value 属性 之外 class 在 MuleESB 实现 Callable 接口?在我的例子中 some_property:
- 在 MainClass 的 onCall 方法中不为空(确定)
- ClassB 中的 foo() 方法为空(错误)
如何在 foo() 方法中获取这个值?我不想将它作为参数传递。
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);
}
}
编辑:关于我的问题的更多信息
我的 MuleESB 项目包含位于以下文件中的资源文件:
/MY_PROJECT/src/main/resources/services.属性
它包含一些值,例如
some_property = AAAA
another_property = BBBB
此文件在我的 flow.xml 个文件中引用:
/MY_PROJECT/src/main/app/common.xml
<context:property-placeholder location="classpath:services.properties"
order="4" ignore-unresolvable="true" />
我想在另一个流程中使用这个配置。我有 Java 组件有一个 class 实现了 Callable 接口
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
}
}
我有第二个class ClassB,会经常用到。我想 运行 在前面提到的 MainClass
的 onCall() 方法中使用它
public ClassB {
@Value("${some_property}")
String some_property;
public void foo() {
System.out.println(some_property);
}
}
我想从我的 services.properties 文件中加载 some_property 值。我使用下面的代码
@Value("${some_property}")
String some_property;
我只想在 ClassB foo() 方法中读取这个值。但是现在有 null(而不是设置值的 MainClass onCall() 方法)。 ClassB 是独立的,可以在多个 onCall() 方法中使用,因此我不想在 MainClass 中读取此属性并将它们传递给 ClassB 构造函数或方法。我怎样才能做到这一点?
请尝试使用方法注入,您现在可以在外部调用 MainClass.some_property。
public static String some_property;
@Value("${some_property}")
public void setSome_property(String prop) {
some_property = prop;
}
我只是使用构造函数来读取 props 文件。如果您正在创建许多 ClassB 对象,您可能希望更改此读取文件一次,而不是在每个新实例上。
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);
}
}
如何读取 @Value 属性 之外 class 在 MuleESB 实现 Callable 接口?在我的例子中 some_property:
- 在 MainClass 的 onCall 方法中不为空(确定)
- ClassB 中的 foo() 方法为空(错误)
如何在 foo() 方法中获取这个值?我不想将它作为参数传递。
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);
}
}
编辑:关于我的问题的更多信息
我的 MuleESB 项目包含位于以下文件中的资源文件:
/MY_PROJECT/src/main/resources/services.属性
它包含一些值,例如
some_property = AAAA
another_property = BBBB
此文件在我的 flow.xml 个文件中引用:
/MY_PROJECT/src/main/app/common.xml
<context:property-placeholder location="classpath:services.properties"
order="4" ignore-unresolvable="true" />
我想在另一个流程中使用这个配置。我有 Java 组件有一个 class 实现了 Callable 接口
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
}
}
我有第二个class ClassB,会经常用到。我想 运行 在前面提到的 MainClass
的 onCall() 方法中使用它public ClassB {
@Value("${some_property}")
String some_property;
public void foo() {
System.out.println(some_property);
}
}
我想从我的 services.properties 文件中加载 some_property 值。我使用下面的代码
@Value("${some_property}")
String some_property;
我只想在 ClassB foo() 方法中读取这个值。但是现在有 null(而不是设置值的 MainClass onCall() 方法)。 ClassB 是独立的,可以在多个 onCall() 方法中使用,因此我不想在 MainClass 中读取此属性并将它们传递给 ClassB 构造函数或方法。我怎样才能做到这一点?
请尝试使用方法注入,您现在可以在外部调用 MainClass.some_property。
public static String some_property;
@Value("${some_property}")
public void setSome_property(String prop) {
some_property = prop;
}
我只是使用构造函数来读取 props 文件。如果您正在创建许多 ClassB 对象,您可能希望更改此读取文件一次,而不是在每个新实例上。
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);
}
}