如何从自定义java组件中的上下文属性占位符标记获取属性

Ang*_*mad 1 spring mule

我有定义的Mule配置

<context:property-placeholder location="classpath:/mule-sw.properties"/>
Run Code Online (Sandbox Code Playgroud)

我还有一个Java自定义组件类,它在我的一个字段上使用@Lookup注释

@Lookup("file-path")
private String path;
Run Code Online (Sandbox Code Playgroud)

考虑到我的"mule-sw.properties"是这样的

file-path=C:/hello.txt
Run Code Online (Sandbox Code Playgroud)

在我启动Mule App之后,我总是得到Deploy Exception

org.mule.api.expression.RequiredValueException: "Required object not found in registry of Type "class java.lang.String" with name "file-path" on object "class component.CustomComponent"
Run Code Online (Sandbox Code Playgroud)

我还尝试使用@Lookup("$ {file-path}")更改@Lookup("文件路径")但没有成功.

谁能给我一个更好的解决方案?任何帮助都非常感谢.

谢谢

gen*_*nzo 7

@Lookup注释用于从注册表中检索对象,而您尝试执行的操作是为自定义组件的属性赋值.

有两种方法可以做到这一点:

1)通过属性注入,即您声明组件如下:

<custom-component class="org.myCompany.CustomComponent">
    <property name="file-path" value="${file-path}" />
</custom-component>
Run Code Online (Sandbox Code Playgroud)

2)通过Spring EL支持和@Value注释,即以下列方式注释属性

@Value("${file-path}")
private String path;
Run Code Online (Sandbox Code Playgroud)

我推荐第一种方法,因为从流程角度来看它更容易维护