2017-08-15 73 views
0

在我的項目中我有一個基於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。

我感謝所有幫助

+1

和使用'@ ConfigurationProperties'你需要指定'service.assets.url'你的屬性文件,不只是'service.assets'。 – alfcope

回答

相關問題