第一种方式 :
java工具类读取配置文件工具类
只是案例代码 抓取异常以后的代码自己处理
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertyUtil { private static Properties props; static{ loadProps(); } synchronized static private void loadProps(){ props = new Properties(); InputStream in = null; try { in = PropertyUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); props.load(in); } catch (FileNotFoundException e) { } catch (IOException e) { } finally { try { if(null != in) { in.close(); } } catch (IOException e) { } } } public static String getProperty(String key){ if(null == props) { loadProps(); } return props.getProperty(key); } public static String getProperty(String key, String defaultValue) { if(null == props) { loadProps(); } return props.getProperty(key, defaultValue); } }
第二种方式:
通过spring注入的方式
通过参入注入即可
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/classes/dbconfig.properties</value> <value>classpath:redisconfig.properties</value> <value>classpath:postconfig.properties</value> </list> </property> </bean> <bean class="com.skjd.bean.PostCongfig"> <property name="posturl" value="${posturl}" /> </bean>