この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:13
ページ更新者:guest
更新日時:2026-06-11 07:12:00

タイトル: classpath: 内部ファイルの読み込み
SEOタイトル: Spring classpath: プレフィックス|src/main/resources のリソース読み込み

この記事の要点
  • Spring の classpath: プレフィックスはプロジェクト内リソースを指定する識別子
  • 示す場所: src/main/resources/(Maven / Gradle 標準レイアウト)
  • file: との違い: classpath: は jar 内部、file: は外部(フルパス)
  • 書式: @PropertySource("classpath:/jdbc.properties") / ResourceLoader.getResource("classpath:foo.txt")
  • classpath*: は複数 jar 横断検索(プラグイン読み込みなどで使用)

classpath: とは

classpath: は Spring Framework が提供するリソース指定の識別子です。プロジェクトの src/main/resources/ 以下にあるファイルを示します。ビルド後は jar / war 内部に含まれるため、配布した後でもアプリ内から読み込めます。

プロジェクト構造

your-project/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/App.java
│   │   └── resources/      ← ★ classpath: が指す場所
│   │       ├── application.properties
│   │       ├── jdbc.properties
│   │       ├── mybatis-config.xml
│   │       └── data/
│   │           └── master.csv
│   └── test/
│       └── resources/      ← テスト時の classpath: もここを含む
├── pom.xml
└── target/
    └── classes/            ← ビルド後はここに resources がコピーされる

file: との違い

プレフィックス指す場所用途
classpath:jar / war 内部(src/main/resources)定数・設定・テンプレート等、配布物に含めるファイル
classpath*:クラスパス全体(複数 jar を横断)プラグイン的に複数モジュールの設定を統合
file:OS のファイルシステム(絶対 / 相対パス)外部設定ファイル(環境ごとに変える設定など)
http: / https:リモート URL稀に外部リソースの読み込み
プレフィックスなし環境に依存(通常 file:)非推奨。明示すべき

使用例 1: プロパティファイル読み込み

@Configuration
@PropertySource("classpath:/jdbc.properties")
public class DbConfig {
    @Value("${db.url}")
    private String dbUrl;

    @Value("${db.user}")
    private String dbUser;
}

Spring Boot の application.properties は明示的に classpath: を書かなくても自動で読み込まれます。追加のプロパティを読みたいときに使います。

使用例 2: ResourceLoader で読み込み

@Service
public class MasterDataService {

    @Autowired
    private ResourceLoader resourceLoader;

    public List<String> readMasterCsv() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:data/master.csv");
        try (BufferedReader br = new BufferedReader(
                new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
            return br.lines().collect(Collectors.toList());
        }
    }
}

使用例 3: テンプレート/XML 読み込み

<!-- mybatis-spring 設定 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>

<!-- Velocity / Thymeleaf テンプレートの場所 -->
<property name="templateLoaderPath" value="classpath:/templates/"/>

classpath* の使い方(複数 jar 横断)

プラグイン的に複数 jar から同名のリソースを集めたい場合に classpath*: を使います:

// 全 jar の META-INF/spring.factories を集める例
Resource[] resources = applicationContext.getResources(&quot;classpath*:META-INF/spring.factories&quot;);

// XML 設定を複数モジュールから集める
@ImportResource(&quot;classpath*:META-INF/spring/*-context.xml&quot;)

jar 化後の注意点

classpath: リソースは jar 内部にあるため、通常の File オブジェクトとして開けません:

// ❌ NG: jar 内部のリソースは File として扱えない
Resource resource = resourceLoader.getResource(&quot;classpath:data/master.csv&quot;);
File file = resource.getFile();  // FileNotFoundException

// ✅ OK: InputStream で読む
InputStream is = resource.getInputStream();

// ✅ OK: 一時ファイルにコピーする
File tmp = Files.createTempFile(&quot;master&quot;, &quot;.csv&quot;).toFile();
Files.copy(is, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);

環境別の設定切り替え

本番では file: で外部設定を読みたいケース。両方をフォールバック構成にできます:

@PropertySource(value = &quot;classpath:application-default.properties&quot;)
@PropertySource(value = &quot;file:${CONFIG_DIR}/application.properties&quot;, ignoreResourceNotFound = true)
public class AppConfig {}

環境変数 CONFIG_DIR に本番設定ディレクトリを指定。ファイルが無ければ classpath 内部設定だけが使われる、というフォールバック。

よくあるハマりどころ

  • "classpath:foo.txt" vs "classpath:/foo.txt": 先頭の / は推奨だが省略可能。意味は同じ
  • jar 内部で File として開こうとして失敗: 必ず InputStream で読む
  • テスト時のリソースが本番に混入: src/test/resources のファイルは jar に含まれない(テスト時のみ classpath)
  • パッケージング後にリソースが見つからない: pom.xml<resources> 設定を確認 / Maven 標準なら設定不要
  • Windows でパス区切り: classpath では / 統一(\ は使わない)