ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|---|
|
file: / classpath: プレフィックス
| プレフィックス | 用途 | 例 |
|---|---|---|
classpath: | クラスパス (jar 内含む) | classpath:config/app.json |
classpath*: | 複数 jar から検索 | classpath*:META-INF/spring.factories |
file: | ファイルシステム | file:/etc/myapp/config.json |
http:// / https:// | リモート URL | https://example.com/config.json |
url: | generic URL | url:ftp://... |
| プレフィックスなし | 相対パス(カレントディレクトリ) | config/app.json |
方法 1: @Value で Resource 注入
@Service
public class ConfigService {
@Value("classpath:config/app.json")
private Resource configResource;
@Value("file:/etc/myapp/secret.key")
private Resource secretResource;
public String readConfig() throws IOException {
try (InputStream is = configResource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
}
}
}
方法 2: ResourceLoader 注入
@Service
public class FileService {
@Autowired
private ResourceLoader resourceLoader;
public String loadFile(String path) throws IOException {
Resource resource = resourceLoader.getResource(path);
// path: "classpath:config/app.json"
// "file:/etc/myapp/data.txt"
// "https://example.com/data.json"
try (InputStream is = resource.getInputStream()) {
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
}
}
}
方法 3: ClassPathResource 直接
// classpath: のみ
ClassPathResource resource = new ClassPathResource("config/app.json");
try (InputStream is = resource.getInputStream()) {
ObjectMapper mapper = new ObjectMapper();
AppConfig config = mapper.readValue(is, AppConfig.class);
}
// Path 指定 (Spring Boot 標準の resources/ フォルダ基準)
ClassPathResource resource = new ClassPathResource("/templates/email.html");
方法 4: ResourceUtils.getFile()
// java.io.File として取得 (開発時のみ動く、jar 内では NG)
try {
File file = ResourceUtils.getFile("classpath:config/app.json");
String content = Files.readString(file.toPath());
} catch (FileNotFoundException e) {
// jar 内では FileNotFoundException
// → 本番では getInputStream() 推奨
}
// 安全な書き方
Resource resource = new ClassPathResource("config/app.json");
String content;
try (InputStream is = resource.getInputStream()) {
content = new String(is.readAllBytes(), StandardCharsets.UTF_8);
}
方法 5: PropertyPlaceholderConfigurer / @PropertySource
@Configuration
@PropertySource("classpath:custom.properties")
@PropertySource(value = "file:${EXTERNAL_CONFIG:/etc/myapp/config.properties}",
ignoreResourceNotFound = true)
public class AppConfig { }
方法 6: spring.config.import (Spring Boot 2.4+)
# application.yml
spring:
config:
import:
- classpath:custom.properties
- file:/etc/myapp/override.yml
- optional:configserver:http://config-server
- vault://
用途別の選び方
| 用途 | 推奨 |
|---|---|
| プロパティ設定読込 | application.properties + @Value |
| テンプレート / 静的ファイル | @Value("classpath:templates/...") |
| 外部設定ファイル(環境別) | spring.config.import |
| 暗号化された秘密情報 | Vault / AWS Secrets Manager |
| 大きなデータファイル | file: + StreamingResponseBody |
InputStream vs File
// ✅ jar 内・ファイルシステム両方 OK
Resource resource = new ClassPathResource("config/app.json");
try (InputStream is = resource.getInputStream()) {
// 処理
}
// ❌ jar 内では動かない (FileNotFoundException)
File file = ResourceUtils.getFile("classpath:config/app.json");
// 一時ファイルに展開する例
Resource resource = new ClassPathResource("template.docx");
Path tempFile = Files.createTempFile("template", ".docx");
try (InputStream is = resource.getInputStream()) {
Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING);
}
// tempFile に対して通常の File API が使える
具体例: テンプレートファイル読込
// resources/templates/email_welcome.html を読む
@Service
public class EmailService {
@Value("classpath:templates/email_welcome.html")
private Resource welcomeTemplate;
public String renderWelcomeEmail(String userName) throws IOException {
try (InputStream is = welcomeTemplate.getInputStream()) {
String template = new String(is.readAllBytes(), StandardCharsets.UTF_8);
return template.replace("{{name}}", userName);
}
}
}
具体例: 外部 JSON 設定
// 開発: classpath、本番: 外部ファイル
@Configuration
public class ExternalConfig {
@Value("${myapp.config.path:classpath:config/default.json}")
private Resource configResource;
@Bean
public AppSettings appSettings() throws IOException {
try (InputStream is = configResource.getInputStream()) {
return new ObjectMapper().readValue(is, AppSettings.class);
}
}
}
# application.properties
myapp.config.path=file:/etc/myapp/production.json
# または環境変数: MYAPP_CONFIG_PATH=...
テスト時の差し替え
@SpringBootTest
@TestPropertySource(properties = "myapp.config.path=classpath:config/test.json")
class MyServiceTest { ... }
// または application-test.yml
myapp:
config:
path: classpath:config/test.json
関連記事
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
- インストール(eclipseプラグイン)
- クイックスタート
- プロジェクトの作成
- Spring Bootプロジェクトの作成
- Spring Bootプロジェクトの実行
- Spring BootでHello World!
- アノテーション一覧
- DB接続設定からエンティティおよびリポジトリの作成、値の取得まで(JPA編)
- DB接続設定や値の取得(JdbcTemplate編)
- ビューから値をモデルに格納しコントローラーで受け取る方法
- コントローラーにてモデルに値を格納してビューに渡す方法
- テンプレートエンジン
- ModelとModelAndViewの違い
- AOPの使用方法
- classpath: 内部ファイルの読み込み
- file: 外部ファイルの読み込み
- CSVファイルアップロード方法(Ajax)
- CSVファイルダウンロード方法(Ajax)
- Spring Bootプロジェクトのビルドと本番環境へのデプロイ方法(内部tomcat使用)
- Application.propertiesの環境依存設定の分割方法
- JPAにおけるEntityManagerの取得方法
- JPAにおけるjava.sql.Connectionの取得方法
- エラー一覧
- jarの引数を受け取る方法
- Spring BootでGmailからメール送信
- 複数のDBに接続する設定(Spring Boot & JPA編)
- ポート番号の変更
- Basic認証の実装と特定のURLに限定する方法
- Spring SecurityのBasic認証の無効化
- 独自のエラーページを定義する方法
- プロパティファイルの値やjar実行時の引数を取得する方法
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Laravel キャッシュクリア完全ガイド(cache:clear / config:clear / 2026-05-18 07:42:07
- プロジェクトの作成と削除 2026-05-18 07:42:07
- インストール直後にNetbeansが反応しない 2026-05-18 07:42:07
- 動画やチャンネルの検索 2026-05-18 07:42:07
- APIキー取得方法 2026-05-18 07:42:07
- チャンネル情報の取得 2026-05-18 07:42:07
- API 入門 — Web API(REST / GraphQL / gRPC / 2026-05-18 07:42:07
- インストール(eclipseプラグイン) 2026-05-18 07:42:07
- Laravel「Dotenv values containing spaces must be surrounded 2026-05-18 07:42:07
- エラー一覧 2026-05-18 07:42:07
- curl: (51) SSL: certificate subject name '~' does not match 2026-05-18 07:42:07
- インストール方法(Windows版) 2026-05-18 07:42:07
- JSONから配列に変換 2026-05-18 07:42:07
- 処理を一定時間待つ 2026-05-18 07:42:07
- A non well formed numeric value encountered 2026-05-18 07:42:07
コメントを削除してもよろしいでしょうか?