1、方式一:通过spring框架 PropertyPlaceholderConfigurer 工具实现
classpath:conf/jdbc.properties UTF-8 jdbc.properties文件: database.connection.driver=com.mysql.jdbc.Driver database.connection.url=jdbc:mysql:/ //存取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 (); 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"));
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



