一、基于XML配置的方式

1.使用 PropertyPlaceholderConfigurer

- 在 applicationContext.xml 中配置:

<context:property-placeholder location="classpath*:db.properties"/>

或者:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath*:db.properties" ></property>
</bean>

- 之后就可以在代码中访问了:

@Component
public class TestComponent {
@Value("${jdbc.url}")
private String url;
}

2.使用 PropertiesFactoryBean

- 注册 bean

<bean id="dbProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:db.properties</value>
</list>
</property>
</bean>

- 使用

private String url;

@Resource(name = "dbProperties")
private Properties properties; @PostConstruct
public void init() {
url = properties.getProperty("jdbc.url");
}

3.使用 ResourceBundleMessageSource

- 注册 bean

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>

- 访问
可以使用如下方法来访问:

((ApplicationContext)context).getMessage("jdbc.url", null, null);

或者:

@Component
public class BeanTester {
@Autowired
private MessageSource messageSource; public void execute(){
String url = this.messageSource.getMessage("jdbc.url", null, null);
}
}

二、基于Java配置的方式

1.使用 PropertySource

1.1通过 Environment 来获取

@Configuration
// 另有 PropertySources 包含多个 @PropertySource 来配置多个配置文件
@PropertySource("classpath:db.properties")
public class PropertySource {
@Autowired
Environment env; public void execute() {
// 你可以按照如下的方式获取属性值
String url = this.env.getProperty("jdbc.url");
}
}

1.2通过 PropertySourcesPlaceholderConfigurer 来获取

@Configuration
@PropertySource("classpath:db.properties")
public class PropertySource {
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}

调用:

@Component
public class TestComponent {
@Value("${jdbc.url}")
private String url;
}

2.使用 PropertiesFactoryBean

- 注册 bean:

@Bean
public PropertiesFactoryBean propertiesFactoryBean() {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
// 可以配置多个配置文件,也可以使用setLocation值设置单个配置文件
Resource[] resources = new ClassPathResource[]{new ClassPathResource("db.properties")};
propertiesFactoryBean.setLocations(resources);
return propertiesFactoryBean;
}

- 使用:

@Component
public class BeanTester {
private String url; @Resource(name = "propertiesFactoryBean")
private Properties properties; @PostConstruct
public void init() {
// 取到jdbc.url对应的值并赋给了全局变量url
url = properties.getProperty("jdbc.url");
}
}

三、SpringBoot

1. application.xml (或.yml) 中有如下配置内容:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test

1.1 可以直接通过 @Value 注解获取:

@SpringBootApplication
@RestController
public class Applaction {
@Value("${spring.datasource.url}")
private String url;
}

1.2可以通过 Environment 来获取:

@SpringBootApplication
@RestController
public class Applaction {
@Autowired
private Environment env; public void test() {
// 获取配置文件中的属性
env.getProperty("spring.datasource.url");
}
}

2.如果是用户自定义的配置文件,我们可以使用 @ConfigurationProperties 注解来获取:

如你在resources/resources下有一个配置文件 author.properties:

author.name=EricChan
author.sex=male

我们可以通过 @ConfigurationProperties 来将 properties 属性和一个 Bean 及其属性关联,从而实现类型安全的配置:

@Getter
@Setter
@ToString
@Component
@PropertySource(value = "classpath:resources/author.properties", encoding = "UTF-8")
@ConfigurationProperties
public class Author { private String name; private String sex; }

之后我们就可以通过将该 Bean 注入到其他需要使用的地方就可以获取了,比如:

@RestController
public class TestController {
public final Author author; // 将该 bean 注入进来
public TestController(final Author author) {
this.author = author;
} public void test() {
System.out.println(author);
}
}

还有种通过注册监听器的方式可以来实现,实现 ApplicationListener<ApplicationStartedEvent>,但觉得该方式比较麻烦,不太实用,在这里暂不做介绍,有兴趣的可以自己百度谷歌就行了。

最新文章

  1. js判断只能输入数字和只能输入
  2. android添加第三方字体并设置的简单使用
  3. dp88dp6最靠谱的网络赚钱方法
  4. POJ - 1511 Invitation Cards(Dijkstra变形题)
  5. Cfree
  6. HTML meta viewport属性详解
  7. [Java解惑]类
  8. Json数据,日期的转换
  9. opensuse 安装 Anaconda3 之后出现Could not start d-bus. Can you call qdbus?
  10. 【数学,方差运用,暴力求解】hdu-5037 Galaxy (2014鞍山现场)
  11. 在线HTML编辑器 kindeditor-4.1.10 上传图片文件 应用指南
  12. 【技术贴】关闭CMD错误提示声音
  13. 【C++小白成长撸】--(续)双偶数N阶魔阵
  14. 奥利奥好吃吗?Android 8.0新特性适配测试报告来啦!
  15. Java NIO FileVisitor 高效删除文件
  16. Android进阶:四、RxJava2 源码解析 1
  17. Vue技术内幕 出去看看吧 实例化+挂载
  18. Codeforces Round #506 (Div. 3) C. Maximal Intersection
  19. Day7作业及默写
  20. 【noip模拟赛3】编码

热门文章

  1. javolution-core-java-6.1.0.jar 的使用
  2. JavaScript对JSON数据进行排序
  3. 【译】PHP中的Session及其一些安全措施
  4. mysql-connector-java-6日期存储时差的问题解决方法
  5. camera主观测试经验分享.ppt33页
  6. POJ 3020 Antenna Placement 最大匹配
  7. How Tomcat Works 读书笔记 八 加载器 上
  8. Gitlab备份、迁移、恢复和升级
  9. 推荐一个静态页面生成工具-mkdocs
  10. RocketMQ的异步调用