读取配置文件.property

在项目中有时需要配置.property文件

xx.properties配置文件中有数据:

class.name=com.Fruit

class.field=pulp

class.method=eat

1、项目工程中使用注解@Value方式

在web项目工程中可以通过@Value("${xx.xx}")来获取.property中配置的

@Value("${class.name}")

private String className;

2、在项目中不适用注解方式

public class TestProperties

    Properties pro = new Properties();//创建Properties对象

   ClassLoader classLoader = TestProperty.class.getClassLoader();//使用类加载器

   InputStream inputStream = classLoader.getResourceAsStream("xx.properties");//获取class目录下的配置文件

   pro.load(inputStream);//加载

   //获取配置文件中的数据

   String className = pro.getProperty("class.name");

   String field = pro.getProperty("class.field");

   String method = pro.getProperty("class.method");

 

posted @ 2020-05-05 15:48  ASKANDANSWERS  阅读(121)  评论(0编辑  收藏  举报