独立的骆驼-在@ BeanInject'ed bean上无法配置PropertiesComponent

时间:2019-05-10 16:32:23

标签: apache-camel

注意:问题是关于未发布的Camel 3.0.0-M2版本的独立(使用Main类)行为,该版本相对于Camel 2.x具有独立模式的许多增强功能-以下代码不适用于Camel 2。 x

问题:我注意到[1]中描述的对属性组件的修改不会影响注入到bean中的配置属性。就我而言,我想将PC的environmentVariableMode设置为覆盖(默认为fallback)。

虽然覆盖在路由上有效,但是Bean注入了原始属性值。

application.properties中的属性“ hi”设置为“ Hello”,环境变量“ hi”设置为“ Huhu”,当将environmentVariableMode设置为覆盖(2)时,该参数应覆盖前者。

运行时:

System env var hi=Huhu
14:34:02.282 [Camel (camel-1) thread #2 - timer://foo] INFO route1 - Huhu from route
14:34:02.297 [Camel (camel-1) thread #2 - timer://foo] INFO route1 - Hello from bean

代码:

public class MyApplication {
    private MyApplication() {
    }

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.addConfigurationClass(MyConfiguration.class);
        main.addRouteBuilder(MyRouteBuilder.class);
        main.run(args);
    }
}

public class MyConfiguration {
    @BindToRegistry
    public MyBean myBean(@PropertyInject("hi") String hi) {
        return new MyBean(hi);
    }
}

public class MyBean {
    private String hi;

    public MyBean(String hi) {
        this.hi = hi;
    }

    public String hello() {
        return hi + " from bean";
    }
}

public class MyRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        CamelContext context = getContext();
        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
        pc.setEnvironmentVariableMode(PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE);  //default is FALLBACK

        System.out.println("System env var hi=" + System.getenv("hi"));

        from("timer:foo?repeatCount=1")
            .log("${properties:hi} from route")
            .bean("myBean")
            .log("${body}");
    }
}

application.properties:
hi = Hello

使它起作用的唯一方法是重写Main#postProcessCamelContext-这真的是它打算使用的方式吗?还是有一种更惯用的方式?

public class MyApplication extends Main {
    private MyApplication(String[] args) throws Exception {
        addConfigurationClass(MyConfiguration.class);
        addRouteBuilder(MyRouteBuilder.class);
        run(args);
    }

    @Override
    protected void postProcessCamelContext(CamelContext camelContext) throws Exception {
        PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class);
        pc.setEnvironmentVariableMode(PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE);
        super.postProcessCamelContext(camelContext);
    }

    public static void main(String[] args) throws Exception {
        new MyApplication(args);
    }
}

对Camel开发的建议:将environmentVariableMode设置为默认覆盖而不是回退不是更有意义,尤其是在考虑容器部署时:环境变量优先于系统属性,系统属性优先于应用程序配置(例如应用程序) .properties)?

[1] https://github.com/apache/camel/blob/master/components/camel-properties/src/main/docs/properties-component.adoc

1 个答案:

答案 0 :(得分:1)

是的,最好覆盖ENV,欢迎您登录JIRA并在github PR上工作。我们热爱贡献 http://camel.apache.org/support.html

我记录了一张票:https://issues.apache.org/jira/browse/CAMEL-13502

好的,现在已经实现为默认模式,您还可以从application.properties文件等中进行配置:https://github.com/apache/camel/blob/master/examples/camel-example-main/src/main/resources/application.properties#L23

在骆驼v3.0M3中,@PropertyInject的问题也已解决