繁体   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