Spring外部化配置
3415 ワード
外部化配置とは、プロジェクトの配置情報をすべてappication Contectの配置ファイルに混ぜないことを意味します.データベースの配置情報、システムパスの配置情報など、これらの情報は独立して、システム構成員や後期にキー情報を維持する便利さを保つべきです.
例えば$userはroot$である.driverClassはcomp.mysql.jdbc.Driverである.
外部ファイルのappicationContectを参照する場合、コードセグメントを設定します.
上記は使い方です.ファイルを導入する方法1:
方法二:
config.txt
まとめ:
c3p0.properties
driverClass=com.mysql.jdbc.Driver
user=root
password=root
acquireIncrement=5
...
その設定情報をappication Contectの設定ファイルに導入する方法は二つあります.導入後、appication Contectで$keyを使ってこのキーの値にアクセスできます.例えば$userはroot$である.driverClassはcomp.mysql.jdbc.Driverである.
外部ファイルのappicationContectを参照する場合、コードセグメントを設定します.
<bean id="resourceLoaderConfig" class="com.chapter2p10.ResourceLoaderConfig" >
<property name="resource">
<value>${filepath}</value> <!-- filepath -->
</property>
</bean>
上記は使い方です.ファイルを導入する方法1:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/zk/config.txt</value>
</property>
</bean>
PropertyPlaceholderConfigrerを使用して、location属性は一つのプロファイルを指定します.locations属性は複数のプロファイルを指定します.方法二:
<context:property-placeholder location="classpath:com/zk/config.txt"/>
複数のファイルは英字コンマで区切られます.config.txt
filepath=file:e:/hello.txt
まとめ:
PropertyPlaceholderConfigurer , , 。 bean。 。