Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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
Java Spring两个不同的应用程序上下文-属性占位符冲突_Java_Spring_Dependency Injection_Spring Boot - Fatal编程技术网

Java Spring两个不同的应用程序上下文-属性占位符冲突

Java Spring两个不同的应用程序上下文-属性占位符冲突,java,spring,dependency-injection,spring-boot,Java,Spring,Dependency Injection,Spring Boot,我已经使用Spring框架创建了一个SDK 要与REST后端集成,请使用依赖项注入 在这个SDK中,我有MapPropertySources来处理PropertyPlaceHolders。 基本上,我以编程方式在那里注册一些我想要的属性 要在SDK中使用@Value注释进行解析 它在SDK中运行良好,但是当我构建SDK(使用构建器)时 在Spring boot应用程序中,来自MapPropertiesPlaceHolder的属性 这些问题不再得到解决 我从builder类中获得了以下代码:

我已经使用Spring框架创建了一个SDK 要与REST后端集成,请使用依赖项注入

在这个SDK中,我有
MapPropertySources
来处理
PropertyPlaceHolders
。 基本上,我以编程方式在那里注册一些我想要的属性 要在SDK中使用
@Value
注释进行解析

它在SDK中运行良好,但是当我构建SDK(使用构建器)时 在
Spring boot
应用程序中,来自
MapPropertiesPlaceHolder的属性
这些问题不再得到解决

我从builder类中获得了以下代码:

    public MyRepository build() {


    AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext();

    StandardEnvironment env = new StandardEnvironment();
    context.setEnvironment(env);


    Map<String, Object> propertiesMap = new HashMap<>();
    propertiesMap.put("baseUrl", baseUrl);

    MutablePropertySources mutablePropertySources = context.getEnvironment().getPropertySources();
    mutablePropertySources.addFirst(new MapPropertySource("customPropertiesMap", propertiesMap));


    if(jerseyClient == null){
        jerseyClient = JerseyClientBuilder.createClient();
    }

    context.getBeanFactory().registerSingleton("jerseyClient", jerseyClient);

    context.setParent(null);
    context.register(MySdk.class);
    context.refresh();

    MySdk mySdk = new MySSdk(context);

    return mySdk;
}
在同一个SDK中的测试中,一切正常。这个
baseUrl
属性解析得很好,但当我将此SDK连接到另一个SDK中时 spring应用程序,在生成器中传递的属性,它们不是
@值
注释识别

您是否在Spring配置中定义了
属性资源占位符配置器
bean?每当
@Value
注释中的属性解析失败时,这是首先想到的事情之一

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
指向Javadoc的链接:


hmm刚刚阅读了一些关于利用java配置和属性占位符的有趣内容。查看此链接#3。看起来您必须注册另一个bean


还值得注意的是,有一个关于儿童与父母环境的全范围讨论。我不相信在不创建这种关系的情况下可以有两个上下文。

没有这样加载属性,因为我通常使用属性文件或云配置运行,但是您不应该将MySdk实例注册到上下文而不仅仅是类本身吗?我编辑了文章,还添加了MySdk类。spring处理两个或更多上下文的方式确实让我感到奇怪,因为我遇到了一个问题。很可能,我的SDK的上下文(在另一个spring上下文中实例化)在
AnnotationConfigApplicationContext
中发挥了一些我不知道的魔力。。。我本来希望这个SDK中的上下文与其他上下文隔离,但Spring似乎在背后发挥了太多的魔力……这是对的,我调试的方向是错误的。我猜SpringBoot会自动为您创建这个bean,但是如果您不使用SpringBoot,而只使用SpringFramework,那么您必须创建它。不确定属性源的生命周期到底是怎样的,但问题并不是因为我有两个spring上下文,就像我最初想的那样。
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}