方式一:
//创建Properties对象
Properties prop = new Properties();
//读取classPath中的properties文件
prop.load(ConsumerController.class.getClassLoader().getResourceAsStream("bean.properties"));
//根据键取出值
String className = prop.getProperty("test");//读取bean.properties里面,test的值
==================================================================
方式二:
使用工具类,将属性@Value("server.port)类似赋值,类加@Component以服务注入,获取内容时,只需要@Autowired服务工具
类,调用属性
@Component
@Data
public class Student {
@Value("${server.port}")
private String port;
}
@Autowired
private Student student; 使用时直接注入,然后调用属性值
private static String port; //如果属性是静态的,可以用方
Java配置文件读取技巧
本文介绍了两种Java读取配置文件的方法:一是通过@Service注解和@Autowired注入工具类获取配置内容;二是引入额外的依赖来读取配置。
订阅专栏 解锁全文
134

被折叠的 条评论
为什么被折叠?



