【问题标题】:Getting values from yml configuration to dependency从 yml 配置获取值到依赖项
【发布时间】:2020-02-07 15:57:53
【问题描述】:

我上课了:

@WebServiceClient(name = "${service.name}", targetNamespace = "${service.namespace}", wsdlLocation = "${service.wsdlLocation}")
public class ExampleService extends Service {
    @Value("${service.wsdlLocation}")
    private static String wsdlLocation;
}

它是 mvn 项目的一部分,我从本地 maven repo 编译并使用它作为对我的其他 spring-boot 应用程序的依赖项,该应用程序具有 yml 配置:

service:
  name: name
  namespace: namespace
  wsdlLocation: wsdlLocation

有没有办法让这个 ExampleService 类使用“父”项目的配置?

编辑:

出现了两个答案,但我觉得我没有问清楚。我现在要做的是在我的“父”项目中使用类 ExampleService 并让它看到该父项目的配置。到目前为止,例如我在父项目代码中编写如下:

ExampleService exampleService = new ExampleService();
System.out.println(exampleService.getWsdlLocation());

Null 被打印出来。所以我正在寻找解决方案。

编辑 2: 这是我的课。 父项目:

package com.example.kris.parentservice;

import com.example.kris.childservice.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/example")
public class ExampleController {

//    @Autowired
    private ExampleService exampleService;
    @Value(value = "${service.wsdlLocation}")
    private String wsdlLocation;

    @GetMapping(value = "/example")
    public void example() {
        exampleService = new ExampleService();
        System.out.println("ExampleService field: " + exampleService.getWsdlLocation());
        System.out.println("@Value: " + wsdlLocation);
    }
}

子项目:

package com.example.kris.childservice;

import org.springframework.beans.factory.annotation.Value;

public class ExampleService {
    @Value("${service.wsdlLocation}")
    private String wsdlLocation;

    public String getWsdlLocation() {
        return wsdlLocation;
    }
}

运行父控制器后输出:

ExampleService field: null
@Value:test.wsdl.location

【问题讨论】:

  • 好像 yml 没有加载,您可以在 application.properties 中添加相同的属性以确保选择该属性。
  • Yaml 已加载,我可以在父项目中创建的类中使用其他值。问题在于项目中的类作为依赖项加载。
  • @KrzysztofTkacz 每当您编辑问题时,请尝试向回答的人发表评论,我没有注意到编辑,编辑我的答案,因为 cmets 中的代码变得混乱

标签: java spring spring-boot configuration yaml


【解决方案1】:

YamlPropertySourceLoader 可以帮助 Spring Boot 应用程序注入这样的 bean

@Bean
public PropertySource<?> yamlPropertySourceLoader() throws IOException {
    YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    PropertySource<?> applicationYamlPropertySource = loader.load("application.yml",
            new ClassPathResource("application.yml"), "default");
    return applicationYamlPropertySource;
}

【讨论】:

    【解决方案2】:

    Spring Boot 使用一个非常特殊的 PropertySource 顺序,即 旨在允许明智地覆盖值。属性是 按以下顺序考虑:Source

    在此基础上,您可以决定哪些属性将生效。 如果属性键重复,最后声明的文件将“获胜”并覆盖。

    1. 您的主目录上的 Devtools 全局设置属性(当 devtools 处于活动状态时为 ~/.spring-boot-devtools.properties)。

      1. 您的测试中的@TestPropertySource 注释。
      2. 你的测试的属性属性。可在 @SpringBootTest 和 用于测试您的特定部分的测试注释 应用。
      3. 命令行参数。
      4. 来自 SPRING_APPLICATION_JSON 的属性(嵌入在 环境变量或系统属性)。
      5. ServletConfig 初始化参数。
      6. ServletContext 初始化参数。
      7. 来自 java:comp/env 的 JNDI 属性。
      8. Java 系统属性 (System.getProperties())。
      9. 操作系统环境变量。
      10. 仅在 随机。*。
      11. 打包之外的特定于配置文件的应用程序属性 罐 (应用程序-{profile}.properties 和 YAML 变体)。
      12. 打包在 jar 中的特定于配置文件的应用程序属性
        (应用程序-{profile}.properties 和 YAML 变体)。
      13. 打包 jar 之外的应用程序属性 (application.properties 和 YAML 变体)。
      14. 应用程序属性打包在您的 jar 中
        (application.properties 和 YAML 变体)。
      15. @Configuration 类上的@PropertySource 注释。

      16. 默认属性(通过设置指定 SpringApplication.setDefaultProperties)。

    编辑: 正如您在编辑中提到的,您正在使用以下代码创建新对象

    ExampleService exampleService = new ExampleService();
    System.out.println(exampleService.getWsdlLocation())
    

    这将从弹簧创建对象,你应该使用自动装配

    @Autowired
    private ExampleService exampleService ;
    
    System.out.println(exampleService.getWsdlLocation())
    

    【讨论】:

    • 尝试了你的建议,我得到了:ParentExampleService 中的字段 exampleService 需要一个找不到的“ExampleService”类型的 bean。注入点具有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true) 操作:考虑在您的配置中定义类型为“ExampleService”的 bean。
    • 好吧,我只是想在我的一个控制器中 @Autowire 那个 ExampleService ,我得到了前面评论中提到的错误。
    • 我认为 ExampleService 对象不是由 spring 创建的,是 ExampleService 在您的应用程序正在扫描类的不同包中(它应该与您的 Application 类相同或子包
    • 另外,没有spring注解ExampleService不确定spring是否会为WebServiceClient注解创建对象
    • 我在原始问题中创建了一些示例代码,它提出了我的问题,请看一下。
    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 2016-08-26
    • 2020-02-05
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2013-09-05
    相关资源
    最近更新 更多