2017-08-15 10 views
0

私のプロジェクトでよく使用されるサービスを提供するSpringブートベースのライブラリがあります。 Springブートプロジェクトでは、ライブラリを使用して、いくつかの問題を経験して、いくつかの外部構成を取得しようとしています。Springブート:外部設定の結果が空の値

ライブラリは、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); 
    } 
} 

ザ・春ブーツプロジェクトは、ライブラリをインポートし、外部の設定ファイルを外部のコンフィギュレーションにインポートしたライブラリで定義されてservice.assetsが含まれていapplication.propertiesを設定されています。

@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ブートベースのライブラリを使用するプロジェクトの関連部分は、私の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の値がnullです。 Springブートはファイルapplication.propertiesの外部構成の値を読み込みません。どうして?そして、それを実行するために私はそれをどのように解決できますか?

もう一つのヒント:春ブートプロジェクト内から次のコード行でservice.assetsを取得しようとして

@Value("${service.assets}") 
String url; 

春ブート構成を読み取り、それが適切な値で満たされます。

使用されているSpringブートバージョンは、1.5.3.RELEASEです。

私は助けていただきありがとうございます。

+1

は、あなたのプロパティファイルにConfigurationProperties' @あなたは、 'service.assets.url'を指定するだけではなく、' service.assets'を必要とする '使用してコードに基づいて、ありがとうございます。 – alfcope

答えて

0

@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も参照してください。

関連する問題