【Java】Spring:URLに応じてそれぞれのプロパティファイルから値を取得

■処理概要

指定するURLに応じてそれぞれのプロパティファイルから値を取得する。

URLとプロパティファイルの紐付けは以下の通り。

URLプロパティファイル名
/rest/sample/asampleA.properties
/rest/sample/bsampleB.properties
/rest/sample/csampleC.properties
接続URLとプロパティファイルの紐付け表

「/rest/sample/[a, b, c以外]」が指定された場合は、URLの指定が誤りである旨を通知する。

また上記に共通する値については、「sampleCommon.properties」にて管理するものとする。

▼応答メッセージ仕様

応答メッセージに出力する内容と、対応するプロパティファイルの表を以下に示す。

応答項目プロパティファイル名備考
headersampleCommon.properties
titlesampleA.properties
sampleB.properties
sampleC.properties
「/rest/sample/[a, b, c以外]」が指定された場合は、s指定しない。
datasampleA.properties
sampleB.properties
sampleC.properties
「/rest/sample/[a, b, c以外]」が指定された場合は、s指定しない。
valuesampleA.properties
sampleB.properties
sampleC.properties
「/rest/sample/[a, b, c以外]」が指定された場合は、s指定しない。
messagesampleA.properties
sampleB.properties
sampleC.properties
「/rest/sample/[a, b, c以外]」が指定された場合は、URLの指定が誤りである旨を通知する。
応答項目・プロパティファイル名対応表

▼Java・Spring Bootのバージョン

実装するJavaおよびSpring Bootのバージョンは以下の通り。

項目バージョン
Java17
Spring Boot3.4.2
Java・Spring Bootバージョン

■クラス図

実装するモジュールのクラス図を以下に示す。

■プロパティファイルの設定値

各プロパティファイルに設定する値は以下の通り。

・sampleCommon.properties

sample.common.header=sample common header

・sampleA.properties

sample.a.title=sample A
sample.a.data=hoge A
sample.a.value=1
sample.a.message=OK

・sampleB.properties

sample.b.title=sample B
sample.b.data=hoge B
sample.b.value=2
sample.b.message=OK

・sampleC.properties

sample.c.title=sample C
sample.c.data=hoge C
sample.c.value=3
sample.c.message=OK

各プロパティファイルの配置場所は以下の通り、「resources/conf」配下とする。

■pom.xmlの設定

pom.xmlの設定は以下の通り。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>get-properties-data</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>get-properties-data</name>
	<description>get data from property files.</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

■サンプルコード

実装するモジュールのサンプルコードを以下に示す。

・GetPropertiesController

package com.example.get_properties_data.controller;

import com.example.get_properties_data.dto.PropertiesDTO;
import com.example.get_properties_data.service.RestPropertiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GetPropertiesController {

  @Autowired
  private RestPropertiesService restPropertiesService;

  @GetMapping("/rest/sample/{param}")
  public PropertiesDTO getPropertiesData(@PathVariable String param) {
    return restPropertiesService.getPropertiesDTO(param);
  }
}

・RestPropertiesService

package com.example.get_properties_data.service;

import com.example.get_properties_data.dto.PropertiesDTO;
import java.util.Map;

public interface RestPropertiesService {
  // propertiesの値を取得
  public PropertiesDTO getPropertiesDTO(String params);
}

・RestPropertiesServiceImpl

package com.example.get_properties_data.service;

import com.example.get_properties_data.config.GetSampleAConfig;
import com.example.get_properties_data.config.GetSampleBConfig;
import com.example.get_properties_data.config.GetSampleCConfig;
import com.example.get_properties_data.dto.PropertiesDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class RestPropertiesServiceImpl implements RestPropertiesService {

  @Autowired
  private GetSampleAConfig getSampleAConfig;

  @Autowired
  private GetSampleBConfig getSampleBConfig;

  @Autowired
  private GetSampleCConfig getSampleCConfig;

  @Value("${sample.common.header}")
  private String header;

  /**
   * プロパティ情報取得処理 URLに応じてプロパティの値をセットする
   *
   * @param params: URL:a, b, c, other
   * @return PropertiesDTO
   */
  @Override
  public PropertiesDTO getPropertiesDTO(String params) {

    // 返却用DTO
    PropertiesDTO resultDTO = null;

    // paramごとにプロパティをセットする
    if ("a".equals(params)) {
      resultDTO = getSampleAConfig.setPropertiesSampleADTO();
    } else if ("b".equals(params)) {
      resultDTO = getSampleBConfig.setPropertiesSampleBDTO();
    } else if ("c".equals(params)) {
      resultDTO = getSampleCConfig.setPropertiesSampleCDTO();
    } else {
      resultDTO = setOthers();
    }

    // 共通のヘッダ情報をセットする
    resultDTO.setHeader(header);

    return resultDTO;

  }

  /**
   * プロパティ情報その他のセット処理 - NGのメッセージをセットする
   *
   * @return PropertiesDTO
   */
  private PropertiesDTO setOthers() {

    PropertiesDTO others = new PropertiesDTO();

    // メッセージにNG情報をセット
    others.setMessage("NG: Please choose a, b, or c in URL.");

    return others;

  }
}

・GetSampleAConfig

package com.example.get_properties_data.config;

import com.example.get_properties_data.dto.PropertiesDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:conf/sampleA.properties")
public class GetSampleAConfig {

  @Value("${sample.a.title}")
  private String titleA;

  @Value("${sample.a.data}")
  private String dataA;

  @Value("${sample.a.value}")
  private String valueA;

  @Value("${sample.a.message}")
  private String messageA;

  /**
   * プロパティサンプルA読み込み処理
   *
   * @return PropertiesDTO
   */
  public PropertiesDTO setPropertiesSampleADTO() {

    PropertiesDTO propertiesData = new PropertiesDTO();

    propertiesData.setTitle(titleA);
    propertiesData.setData(dataA);
    propertiesData.setValue(Integer.parseInt(valueA));
    propertiesData.setMessage(messageA);

    return propertiesData;
  }

}

・GetSampleBConfig

package com.example.get_properties_data.config;

import com.example.get_properties_data.dto.PropertiesDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:conf/sampleB.properties")
public class GetSampleBConfig {

  @Value("${sample.b.title}")
  private String titleB;

  @Value("${sample.b.data}")
  private String dataB;

  @Value("${sample.b.value}")
  private String valueB;

  @Value("${sample.b.message}")
  private String messageB;

  /**
   * プロパティサンプルB読み込み処理
   *
   * @return PropertiesDTO
   */
  public PropertiesDTO setPropertiesSampleBDTO() {

    PropertiesDTO propertiesData = new PropertiesDTO();

    propertiesData.setTitle(titleB);
    propertiesData.setData(dataB);
    propertiesData.setValue(Integer.parseInt(valueB));
    propertiesData.setMessage(messageB);

    return propertiesData;
  }

}

・GetSampleCConfig

package com.example.get_properties_data.config;

import com.example.get_properties_data.dto.PropertiesDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:conf/sampleC.properties")
public class GetSampleCConfig {

  @Value("${sample.c.title}")
  private String titleC;

  @Value("${sample.c.data}")
  private String dataC;

  @Value("${sample.c.value}")
  private String valueC;

  @Value("${sample.c.message}")
  private String messageC;

  /**
   * プロパティサンプルC読み込み処理
   *
   * @return PropertiesDTO
   */
  public PropertiesDTO setPropertiesSampleCDTO() {

    PropertiesDTO propertiesData = new PropertiesDTO();

    propertiesData.setTitle(titleC);
    propertiesData.setData(dataC);
    propertiesData.setValue(Integer.parseInt(valueC));
    propertiesData.setMessage(messageC);

    return propertiesData;
  }

}

・GetSampleCommonConfig

package com.example.get_properties_data.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * プロパティサンプル共通読み込みクラス
 */
@Configuration
@PropertySource("classpath:conf/sampleCommon.properties")
public class GetSampleCommonConfig {

}

・HealthCheckDTO

package com.example.get_properties_data.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class PropertiesDTO {
  private String header;
  private String title;
  private String data;
  private int value;
  private String message;
}

■実行結果

・「/rest/sample/a」の場合

{
    "header": "sample common header",
    "title": "sample A",
    "data": "hoge A",
    "value": 1,
    "message": "OK"
}

・「/rest/sample/b」の場合

{
    "header": "sample common header",
    "title": "sample B",
    "data": "hoge B",
    "value": 2,
    "message": "OK"
}

・「/rest/sample/c」の場合

{
    "header": "sample common header",
    "title": "sample C",
    "data": "hoge C",
    "value": 3,
    "message": "OK"
}

・「/rest/sample/s」の場合(a, b, c以外のケース)

{
    "header": "sample common header",
    "title": null,
    "data": null,
    "value": 0,
    "message": "NG: Please choose a, b, or c in URL."
}

コメント