在我的项目中,我有一个基于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。
我感谢任何帮助
答案 0 :(得分:0)
@alfcope:是的,这正是我的问题。属性文件需要指定配置属性&quot; service.assets.url&#39;。在我的属性文件中,只有属性&#39; service.assets&#39;。
有关类型安全配置的详细信息,另请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties。
谢谢