Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 父子上下文中的属性解析顺序_Spring_Configuration - Fatal编程技术网

Spring 父子上下文中的属性解析顺序

Spring 父子上下文中的属性解析顺序,spring,configuration,Spring,Configuration,给定:在运行时创建的应用程序上下文,具有自己的依赖于运行时的属性,以及具有自己的属性和占位符配置器的父应用程序上下文 问题:带有指向父对象链接的子上下文实例化在实例化时会导致占位符解析,这对我来说是完全不可接受的,因为子对象的环境尚未配置 i、 e.进一步的代码压碎了实例化: ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { des

给定:在运行时创建的应用程序上下文,具有自己的依赖于运行时的属性,以及具有自己的属性和占位符配置器的父应用程序上下文

问题:带有指向父对象链接的子上下文实例化在实例化时会导致占位符解析,这对我来说是完全不可接受的,因为子对象的环境尚未配置

i、 e.进一步的代码压碎了实例化:

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { descriptor.getContextFilePath() }, parentContext);

    StandardEnvironment env = new StandardEnvironment();
    Properties props = descriptor.buildProperties(...);
    PropertiesPropertySource pps = new PropertiesPropertySource("endpointContextProps", props);
    env.getPropertySources().addLast(pps);
    context.setEnvironment(env);
    context.refresh();
我找到了一个可行的解决方案,假设不在初始化时设置父上下文,而是在设置了所有必需的环境之后:

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { descriptor.getContextFilePath() }, false);

    StandardEnvironment env = new StandardEnvironment();
    Properties props = descriptor.buildProperties(...);
    PropertiesPropertySource pps = new PropertiesPropertySource("endpointContextProps", props);
    env.getPropertySources().addLast(pps);

    context.setEnvironment(env);
    context.setParent(parentContext);
    context.refresh();
然而,我并不满足于我应该知道我的操作顺序,并且应该记住占位符配置器在上下文创建中是如何工作的。 我想知道是否有更简洁的方法来完成我的上下文工作