1,按格式在properties或yml文件中保存信息
2,将配置文件读入到bean,(注意:在哪个上下文环境装配,spring context还是在springmvc context)
@Bean
public PropertyPlaceholderConfigurer propertyConfigurer(){PropertyPlaceholderConfigurer property=new PropertyPlaceholderConfigurer();
property.setLocation(new ClassPathResource("jdbc.properties"));
property.setFileEncoding("utf-8");
return property;
}
3,在对应的spring context或 mvc context 上下文环境使用@value注入,static属性使用setter方法注入
本文介绍如何在Spring框架中通过properties或yml文件配置信息,并将其读取到bean中进行使用的方法。具体步骤包括:设置PropertyPlaceholderConfigurer以指定配置文件的位置及编码;以及如何在Spring上下文中使用@Value注解来注入这些配置。
828

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



