天天看點

SpringMvc 加載配置.Properties自定義配置檔案的方法

方法一

1.在resources 檔案夾中編寫file.properties

SpringMvc 加載配置.Properties自定義配置檔案的方法

2.在springmvc中編寫:

<bean id="prop" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:file.properties</value>
			</list>
		</property>
	</bean>
           

3.在controller中引用

在Java中使用這個@Value("${ }")注解 讀取 properties中的參數

SpringMvc 加載配置.Properties自定義配置檔案的方法

方法二(推薦用這個,這個用的比較多,也比較簡潔)

<context:property-placeholder location="classpath:file.properties"/>
           

如果有多個自定義配置檔案,也支援通配符

<context:property-placeholder location="classpath:*.properties"/>
           

在Java中使用這個@Value("${ }")注解 讀取 properties中的參數

SpringMvc 加載配置.Properties自定義配置檔案的方法

繼續閱讀