简体   繁体   English

Spring Boot:外部配置导致空值

[英]Spring Boot: External configuration results in empty value

in my project I have a Spring Boot-based library that delivers me some commonly used services. 在我的项目中,我有一个基于Spring Boot的库,可以为我提供一些常用的服务。 In a Spring Boot project I make use of the library and do experience some problems while trying to retrieve some external configuration. 在Spring Boot项目中,我利用该库,在尝试检索某些外部配置时确实遇到了一些问题。

The library "reads" some external configuration within the AssetServiceProperties. 该库“读取” AssetServiceProperties中的一些外部配置。

@ConfigurationProperties("service.assets")
public class AssetServiceProperties {

    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

This class offers those (external) properties in Java representation for the AssetServiceConfiguration. 此类为AssetServiceConfiguration提供Java表示形式的那些(外部)属性。

@Configuration
@EnableConfigurationProperties(AssetServiceProperties.class)
public class AssetServiceConfiguration {
    @Bean
    public AssetServiceClient assetServiceClient(AssetServiceProperties properties) {
        return new AssetServiceClient(properties.getUrl());
    }
}

The AssetServiceClient is the part of the library that is exposed for clients. AssetServiceClient是库中为客户端公开的部分。

public class AssetServiceClient {

    private String url;
    private RestTemplate restTemplate;
    public static final String CONTROLLER = "assets";

    public AssetServiceClient(String url) {
        this.url = url;
        restTemplate = new RestTemplate();
    }

    public AssetsResponse getByService(String service) {
        UriComponentsBuilder builder = UriComponentsBuilder.
            fromUriString(url).
            pathSegment(CONTROLLER).
            queryParam("service", service);

        return restTemplate.getForObject(builder.build().encode().toUri(), AssetsResponse.class);
    }
}

The Spring Boot project imports the library and is configured with an external configuration file application.properties that contains the external configuration service.assets that is defined in the imported library. Spring Boot项目导入该库,并配置了一个外部配置文件application.properties ,其中包含在导入的库中定义的外部配置service.assets

@SpringBootApplication
@EntityScan(basePackageClasses = { Application.class, Jsr310JpaConverters.class })
@Import({AssetServiceConfiguration.class})
@PropertySource(value="classpath:internal.properties")
@PropertySource(value="file:${application.properties}")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

The relevant part of the project that uses the Spring Boot based library is my AssetController. 使用基于Spring Boot的库的项目的相关部分是我的AssetController。

@RestController
@RequestMapping(value = "/assets")
public class AssetController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private AssetServiceClient assetServiceClient;
    private AssetResourceAssembler assetResourceAssembler;

    @Autowired
    public AssetController(
            AssetServiceClient assetServiceClient,
            AssetResourceAssembler assetResourceAssembler) {
        Assert.notNull(assetServiceClient, "assetServiceClient cannot be null");
        this.assetServiceClient = assetServiceClient;
        this.assetResourceAssembler = assetResourceAssembler;
    }

    @GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE, MediaTypes.HAL_JSON_VALUE})
    public ResponseEntity<Resources<AssetResource>> getAll() {
        AssetsResponse assets = assetServiceClient.getByService("tasks");
        return ResponseEntity.ok(assetResourceAssembler.toResourceList(assets.getContent()));
    }
}

The problem definition : When the Spring Boot project code executes, the value of service.assets is null. 问题定义 :执行Spring Boot项目代码时, service.assets的值为null。 Spring Boot does not read the value of the external configuration in the file application.properties . Spring Boot不会在文件application.properties中读取外部配置的值。 Why? 为什么? And how can I solve it to get it run? 我该如何解决才能使其运行?

Another hint: when trying to get the service.assets with the following line of code from within the Spring Boot project 另一个提示:尝试从Spring Boot项目中使用以下代码行获取service.assets

@Value("${service.assets}")
String url;

Spring Boot reads the configuration and gets it filled with a proper value. Spring Boot读取配置,并用适当的值填充它。

The used Spring Boot Version is 1.5.3.RELEASE. 使用的Spring Boot版本是1.5.3.RELEASE。

I appreciate any help 感谢您的帮助

@alfcope: Yes that was exactly my problem. @alfcope:是的,这正是我的问题。 The properties file needs to specify the configuration attribute 'service.assets.url'. 该属性文件需要指定配置属性“ service.assets.url”。 In my properties file there only was the attribute 'service.assets'. 在我的属性文件中,只有属性“ service.assets”。

See also https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties for more details about type safe configuration. 另请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties了解更多详细信息关于类型安全配置。

Thank you 谢谢

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

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