第一种方法:使用注解的方式(可以在代码中直接注解使用,较为方便,推荐使用)
格式如下:@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");