如何从自定义 Java 组件内的上下文属性占位符标记获取属性 [英] How to get property from context property placeholder tag inside custom java component
问题描述
我有定义
<context:property-placeholder location="classpath:/mule-sw.properties"/>
而且我还有一个 Java 中的自定义组件类,它在我的一个字段上使用 @Lookup 注释
And I also have a custom component class in Java which use @Lookup annotation on one of my field
@Lookup("file-path")
private String path;
考虑到我的mule-sw.properties"是这样的
Considering my "mule-sw.properties" is like this
file-path=C:/hello.txt
启动Mule App后,总是出现Deploy Exception
After I start up the Mule App, I always get 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"
我也尝试使用 @Lookup("${file-path}") 更改 @Lookup("file-path") ,但没有成功.
I also tried to change the @Lookup("file-path") with @Lookup("${file-path}") with no success.
谁能给我一个更好的解决方案?任何帮助表示感谢.
Anyone can give me a better solution ? Any help is kindly appreciated.
谢谢
推荐答案
@Lookup 注解旨在检索 注册表中的对象,而您要做的是为自定义组件的属性赋值.
The @Lookup annotation is designed to retrieve objects from the registry , whereas what you are trying to do is to assign a value to an attribute of your custom component.
有两种方法可以做到这一点:
There are 2 way to do that:
1) 通过属性注入,即你像下面这样声明你的组件:
1) Through property injection, i.e. you declare your component like the following:
<custom-component class="org.myCompany.CustomComponent">
<property name="file-path" value="${file-path}" />
</custom-component>
2) 通过 Spring EL 支持和 @Value 注释,即您通过以下方式注释您的属性
2) Through Spring EL support and the @Value annotation, i.e. you annotate your attribute in the following way
@Value("${file-path}")
private String path;
我推荐第一种方法,因为从流程的角度来看它更容易维护
I'd recommend the first approach since it is easier to maintain from a flow perspective
这篇关于如何从自定义 Java 组件内的上下文属性占位符标记获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!