Spring Boot:外部配置导致空值
在我的项目中我有一个基于Spring Boot的库,为我提供了一些常用的服务。在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);
}
}
春季启动项目导入库,并配置了外部配置文件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()));
}
}
问题定义:当春天启动项目的代码执行的service.assets值为空。 Spring Boot不读取文件application.properties中的外部配置的值。为什么?我该如何解决它才能运行?
另一个提示:试图从春天启动项目
@Value("${service.assets}")
String url;
春天引导内得到service.assets与下面的代码时,读取配置,并得到它充满了应有的价值。
使用的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。
谢谢基于您的代码
和使用'@ ConfigurationProperties'你需要指定'service.assets.url'你的属性文件,不只是'service.assets'。 – alfcope