在我的项目中,我有一个基于 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;
}
}
此类以 Java 表示形式为 AssetServiceConfiguration 提供那些(外部)属性。
@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。
我很感激任何帮助