1、读取普通变量
使用@value注解
import org.springframework.beans.factory.annotation.Value;
@Value(value = "${server.port}")
private String result1;
2、读取静态变量
使用@Component注解
3、读取指定配置文件的值
InputStream in = null; Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 in = this.getClass().getClassLoader().getResourceAsStream("generator.properties"); // 使用properties对象加载输入流 try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } // 获取key对应的value值 String jdbcUrl = properties.getProperty("jdbc.connectionURL");