简体   繁体   English

Apache Camel Bean 中的动态 PropertyInjection

[英]Dynamic PropertyInjection in an Apache Camel Bean

I'm using the @PropertyInject annotation to get properties from the application.properties file to use in my beans.我正在使用@PropertyInject批注从 application.properties 文件中获取要在我的 bean 中使用的属性。
This normally works fine, but now I need to be able to change the injected property based on a header value.这通常工作正常,但现在我需要能够根据标头值更改注入的属性。 In my head it looks something like this:在我的脑海中,它看起来像这样:

@PropertyInject(PROPERTY_NAME)  
private String property;

public void pickProperty(String msgVersion) {
        if (msgVersion.equals("A")) {
            PROPERTY_NAME = "property.firstType.name";  
        } else {
            PROPERTY_NAME = "property.secondType.name";
        }
    }

I've considered just injecting both properties and deciding in the main method which one to use, but that seems like a roundabout way of doing things and will get a bit bloated if more versions are added.我考虑过只注入这两个属性并在 main 方法中决定使用哪个,但这似乎是一种迂回的做事方式,如果添加更多版本,则会变得有点臃肿。
Is there an easy way this can be done?有没有一种简单的方法可以做到这一点?

now I need to be able to change the injected property based on a header value现在我需要能够根据标头值更改注入的属性

Properties and Beans are created on application startup and typically do not change while the application is running.属性和 Bean 在应用程序启动时创建,并且在应用程序运行时通常不会更改。 They both have application scope.它们都有适用范围。

Header values on the other hand can change for every message that is processed by your application.另一方面,您的应用程序处理每条消息的标头值都可以更改

As you suggested yourself: You can inject both properties into the Bean and provide a method that is called once per message to get the correct value正如您自己建议的那样:您可以将这两个属性注入 Bean 并提供一个方法,该方法为每条消息调用一次以获取正确的值

@PropertyInject(PROPERTY_A)  
private String propertyA;
@PropertyInject(PROPERTY_B)  
private String propertyB;

// called for every message processing
public String pickProperty(@Header("msgVersion") String msgVersion) {
    if (msgVersion.equals("A")) {
        return propertyA;
    } else {
        return propertyB;
    }
}

This is not at all a workaround, but simply a method that returns a different result based on the input.这根本不是一种解决方法,而只是一种根据输入返回不同结果的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM