Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
通过@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"。
Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置的更多相关文章
- Spring读取外部属性-properties
概述 在Spring中处理外部值最简常用的方法就是外部创建name.properties文件,并在其中声明变量值,供Java进行读取.比如数据源信息配置,Java固定属性位置等.读取的方式一般由三种: ...
- jar读取外部和内部配置文件的问题
最近修改XX应用的时候,涉及到需要在jar包中读取工程配置文件的问题.在jar包中,读取配置文件,需要单独处理. 项目中的一些配置文件,如dbconfig.properties log4j.xml 不 ...
- spring读取properties和其他配置文件的几种方式
1.因为spring容器的一些机制,在读取配置文件进行数据库的配置等等是很有必要的,所以我们要考虑配置文件的的读取方式以及各个方式的实用性 2.配置文件的读取方式我这里介绍2种,目的是掌握这2种就可以 ...
- Spring中注入List,Set,Map,Properties的xml文件配置方法
下面的例子展示了如何注入 List – <list/> Set – <set/> Map – <map/> Properties – <props/> ...
- Struts2 资源配置文件国际化
Struts2 资源配置文件国际化 Struts2资源文件的命名规范:basename_language_country.properties Struts2国际化如果系统同时存在资源文件.类文件,系 ...
- SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...
- spring读取xml配置文件(二)
一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...
- SpringBoot读取外部配置文件的方法
SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...
- Spring使用外部的配置文件
在使用Spring做web项目的时候,通常会使用到数据库的连接信息 jdbcUrl driverClass username password 那么应该如何使用这些属性呢? 如在Spring中使用数据 ...
随机推荐
- 【PHP面试题】通俗易懂的两个面试必问的排序算法讲解:冒泡排序和快速排序
又到了金三银四找工作的时间,相信很多开发者都在找工作或者准备着找工作了.一般应对面试,我们无可厚非的去刷下面试题.对于PHPer来说,除了要熟悉自己所做的项目,还有懂的基本的算法.下面来分享下PHP面 ...
- javaweb开发.常用的第三方包
序号 开发包名称 描述 1 dom4j-1.6.1.jar dom4j用于操作XML文件 2 jaxen-1.1-beta-6.jar 用于解析XPath表达式 3 commons-beanuti ...
- linux获取当前系统的时间
#include <time.h> #include <sys/time.h> void sysLocalTime(char *str_info) { time_t times ...
- [uboot] (第一章)uboot流程——概述
http://blog.csdn.net/ooonebook/article/details/52939100 [uboot] uboot流程系列: [project X] tiny210(s5pv2 ...
- tensorflow学习之(五)构造简单神经网络 并展示拟合过程
# def 添加层 如何构造神经网络 并展示拟合过程 import tensorflow as tf import numpy as np import matplotlib.pyplot as pl ...
- android activity之间用广播传输数据
发送者: Intent intent = new Intent("com.BroadcastAction"); intent.putExtra("result" ...
- nova scheduler filters 配置
scheduler_driver = nova.scheduler.filter_scheduler.FilterScheduler scheduler_default_filters = Avail ...
- 第一篇:服务的注册与发现Eureka(Finchley版本)
一.创建服务注册中心(Eureka) 1. 首先创建一个maven主工程 创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Clo ...
- javascript History对象属性和方法
History对象 History对象包含用户(在浏览器窗口中)访问过的URL length: 返回浏览器历史列表中的URL数量(打开浏览器,访问淘宝,返回1,再访问百度,返回2) History对象 ...
- Tools - Atom编辑器
Atom官网 Atom编辑器的常用插件 预览 document-outline:Show a heirarchical outline of a text document minimap:A pre ...