您的位置:首页 > 运维架构

记录贴:configure.properties等propertie文件的读取问题

2017-10-11 10:01 465 查看
第一种方法:使用注解的方式(可以在代码中直接注解使用,较为方便,推荐使用)

   格式如下:@PropertySource({classpath:文件名})

   @PropertySource({"classpath:configure.properties"})

   如何获取properties文件中的配置项:

   @Value("${db.url}")  

   private String dbUrl;

第二种方式:在spring.xml 文件中申明bean

第三种:在代码中读取

File configFile = null;

Properties props = new Properties();  //Properties 主要用于读取配置文件

configFile = ResourceUtils.getFile("classpath:configure.properties"); //从资源系统中读取到配置文件

props.load(new FileInputStream(configFile));  //load(InputStream inputStream)  从输入流中读取配置文件的各个属性

String dbUrl = props.getProperty("db.url");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: