Spring框架读取property属性文件常用5种方法

时间:2021-05-20

1、方式一:通过spring框架 PropertyPlaceholderConfigurer 工具实现

<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <value>classpath:conf/jdbc.properties</value> </property> <property name="fileEncoding"> <value>UTF-8</value> </property> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> </bean> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${database.connection.driver}"/> <property name="url" value="${database.connection.url}"/> <property name="username" value="${database.connection.username}"/> <property name="password" value="${database.connection.password}"/> </bean> <!-- DAL客户端接口实现-> <bean id="dalClient" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>

2、方式二:简化配置

<beans xmlns="http://mons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${database.connection.driver}"/> <property name="url" value="${database.connection.url}"/> <property name="username" value="${database.connection.username}"/> <property name="password" value="${database.connection.password}"/> </bean> <bean id="dalClient" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>public class PropertyConfigurer extends PropertyPlaceholderConfigurer { //存取properties配置文件key-value结果 private Properties props; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); this.props = props; } public String getProperty(String key) { return this.props.getProperty(key); } public String getProperty(String key, String defaultValue) { return this.props.getProperty(key, defaultValue); } public Object setProperty(String key, String value) { return this.props.setProperty(key, value); } } @Controller @RequestMapping("/") public class TestController { @Autowired PropertyConfigurer propertyConfigurer; @RequestMapping("/index.do") public String getIndex(HttpServletRequest request, HttpServletResponse response, Model model) { Map map = new HashMap<String, Object>(); map.put("orderNo", "111"); String address1 = propertyConfigurer.getProperty("static.picture.address1"); String resRoot = propertyConfigurer.getProperty("resRoot"); String envName = propertyConfigurer.getProperty("database.connection.username"); String keyzjbceshi = propertyConfigurer.getProperty("keyceshi"); map.put("address1", address1); map.put("resRoot", resRoot); map.put("envName", envName); map.put("keyzjbceshi", keyzjbceshi); model.addAllAttributes(map); return "index/index.ftl"; } }

4、方式四:通过ClassPathResource类进行属性文件的读取使用

public class ReadPropertiesUtils1 { private static final Logger LOGGER = LoggerFactory.getLogger(ReadPropertiesUtils1.class); private static Properties props = new Properties(); static { ClassPathResource cpr = new ClassPathResource("conf/ref-system-relation.properties");// 会重新加载spring框架 try { props.load(cpr.getInputStream()); } catch (IOException exception) { LOGGER.error("ReadPropertiesUtils1 IOException", exception); } } private ReadPropertiesUtils1() { } public static String getValue(String key) { return (String) props.get(key); } public static void main(String[] args) { System.out.println("static.picture.address1>>>"+ ReadPropertiesUtils1.getValue("static.picture.address1")); System.out.println("static.picture.address2>>>"+ ReadPropertiesUtils1.getValue("static.picture.address2")); } }

5、方式五:通过ContextClassLoader进行属性文件的读取使用

public class ReadPropertiesUtils2 { private static final Logger LOGGER = LoggerFactory.getLogger(ReadPropertiesUtils2.class); public static String getValue(String key) { Properties properties = new Properties(); try { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/ref-system-relation.properties"); properties.load(inputStream); } catch (FileNotFoundException e) { LOGGER.error("conf/web-sys-relation.properties文件没有找到异常", e); } catch (IOException e) { LOGGER.error("IOException", e); } return properties.getProperty(key); } public static void main(String[] args) { System.out.println("static.picture.address1>>>" + ReadPropertiesUtils2.getValue("static.picture.address1")); System.out.println("static.picture.address2>>>" + ReadPropertiesUtils2.getValue("static.picture.address2")); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章