Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
2024-09-27 05:11:55
通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值;
@PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@Configuration配置类上;
@Value注解可以用在字段和方法上,通常用于从属性配置文件中读取属性值,也可以设置默认值。
具体用法:
@PropertySource(value = { "classpath:config.properties" }, ignoreResourceNotFound = true)
public class UserSpringConfig {
...
}
a、配置多个配置文件
@PropertySource(value = { "classpath:jdbc.properties", "classpath:config.properties"})
public class UserSpringConfig {
...
}
b、忽略不存在的配置文件
@PropertySource(value = { "classpath:jdbc.properties","classpath:config.properties"}, ignoreResourceNotFound = true)
public class UserSpringConfig {
...
}
资源文件配置示例
1、创建配置文件,${project}/src/main/resources/config.properties
username=zhangsan
password=123456
age=20
2、读取外部的资源配置文件
package com.lynch.javaconfig; import org.springframework.beans.factory.annotation.Value; public class User {
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Value("${age}")
private Integer age; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", age=" + age + "]";
} }
3、编写SpringConfig,用于实例化Spring容器
package com.lynch.javaconfig; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; //通过@Configuration注解来表明该类是一个Spring的配置,相当于一个xml文件
@Configuration
@ComponentScan(basePackages = "com.lynch.javaconfig")
@PropertySource(value = { "classpath:config.properties"}, ignoreResourceNotFound = true)
public class UserSpringConfig { @Bean
public User user(){
return new User();
} }
4、编写测试方法,用于启动Spring容器
package com.lynch.javaconfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class UserApplication { public static void main(String[] args) {
// 通过Java配置来实例化Spring容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UserSpringConfig.class); System.out.println(context.getBean(User.class)); // 销毁该容器
context.destroy();
} }
5、执行结果
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
User [username=Administrator, password=123456, age=20]
九月 16, 2018 9:04:45 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
注意,从执行结果发现username输出的是"Administrator",而不是"zhangsan",那是因为系统变量跟配置文件存在相同变量时,优先从系统变量获取,故username输出的是"Administrator"。
最新文章
- [转]eclipse最佳设置
- js中的json
- xmpp的bug
- 回车键转tab键解决方案二
- iOS开发网络篇—文件的上传
- WPF 蒙层罩,正在加载
- left join 等连接查询遇到同名字段覆盖问题
- C++学习笔记:List容器
- TypeScript学习指南第一章--基础数据类型(Basic Types)
- ExtJs5_继承自定义一个控件
- LightOj 1230 Placing Lampposts(树形DP)
- css3圆角代码
- 虚拟键盘冲击移动端fixed布局的解决方案
- ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQL&;EF
- 并行执行 Job - 每天5分钟玩转 Docker 容器技术(134)
- Linux显示inode的信息
- javaScript执行环境、作用域链与闭包
- svn版本提交冲突问题解决详解
- matplotlib -- 基础知识
- python 函数指动态形参,作用域
热门文章
- centos 7安装java开发环境
- C++ 提取网页内容系列之三
- Springboot+Mybatis 显示sql语句
- x64 assembler fun-facts(转载)
- CentOS7:ifconfig command not found解决
- [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creator【s
- shell脚本学习- 传递参数
- java web中的异常处理
- 精选!15个必备的VSCode插件
- WPF 绘制曲线图