この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:4
更新日時:2026-05-15 03:52:15
タイトル: file: 外部ファイルの読み込み
SEOタイトル: 【Spring】file: 外部ファイルの読み込み
| この記事の要点 |
- Spring の
file: プレフィックス: 外部ファイルをフルパス指定で読み込む
- 対比:
classpath: はプロジェクト内(src/main/resources)に限定
- 使い分け:
file: は外部設定 / classpath: は内部リソース
- 書式:
@PropertySource("file:/etc/app/config.properties")
|
Spring Frameworkのfile:に関する記事です。
file:とは
file:はフルパスでファイルを指定します。
リソースファイルの指定はclasspath:も指定できますが、classpath:は自プロジェクト内のファイルに限定されるのに対してfile:は外部ファイルを読み込む場合によく使います。
以下、ファイル指定のサンプルです。
|
@Configuration
@PropertySource({ "file:C:/test/jdbc.properties" })
public class DB2JdbcConfig {
@Autowired
private Environment environment;
@Bean
public DataSource datasource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getProperty("jdbc.url"));
dataSource.setUsername(environment.getProperty("jdbc.username"));
dataSource.setPassword(environment.getProperty("jdbc.password"));
return dataSource;
}
}
|