PropertyPlaceholderConfigurer
Spring では、プロパティ ファイルのロードと解析に使用できます。XML 構成でプロパティ ファイルをロードする手順は次のとおりです。
jdbc.properties 構成ファイルがあるとします。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm753as
jdbc.username=root
jdbc.password=123456
まず、次のようにXML 構成ファイルにcontext
名前空間を導入します。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
次に、 XML 構成ファイルにPropertyPlaceholderConfigurer
Bean 定義を追加し、ロードするプロパティ ファイルのパスを指定します。次に例を示します。
<context:property-placeholder location="classpath:jdbc.properties" />
${}
最後に、次のような構文を使用して、プロパティ ファイル内のプロパティ値を他の Bean 定義で使用できます。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>