簡體   English   中英

Spring Boot:外部配置導致空值

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

在我的項目中,我有一個基於Spring Boot的庫,可以為我提供一些常用的服務。 在Spring Boot項目中,我利用該庫,在嘗試檢索某些外部配置時確實遇到了一些問題。

該庫“讀取” AssetServiceProperties中的一些外部配置。

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

    private String url;

    public String getUrl() {
        return url;
    }

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

此類為AssetServiceConfiguration提供Java表示形式的那些(外部)屬性。

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

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);
    }
}

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);
    }
}

使用基於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()));
    }
}

問題定義 :執行Spring Boot項目代碼時, service.assets的值為null。 Spring Boot不會在文件application.properties中讀取外部配置的值。 為什么? 我該如何解決才能使其運行?

另一個提示:嘗試從Spring Boot項目中使用以下代碼行獲取service.assets

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

Spring Boot讀取配置,並用適當的值填充它。

使用的Spring Boot版本是1.5.3.RELEASE。

感謝您的幫助

@alfcope:是的,這正是我的問題。 該屬性文件需要指定配置屬性“ service.assets.url”。 在我的屬性文件中,只有屬性“ service.assets”。

另請參閱https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties了解更多詳細信息關於類型安全配置。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM