通过@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实现资源文件配置的更多相关文章

  1. Spring读取外部属性-properties

    概述 在Spring中处理外部值最简常用的方法就是外部创建name.properties文件,并在其中声明变量值,供Java进行读取.比如数据源信息配置,Java固定属性位置等.读取的方式一般由三种: ...

  2. jar读取外部和内部配置文件的问题

    最近修改XX应用的时候,涉及到需要在jar包中读取工程配置文件的问题.在jar包中,读取配置文件,需要单独处理. 项目中的一些配置文件,如dbconfig.properties log4j.xml 不 ...

  3. spring读取properties和其他配置文件的几种方式

    1.因为spring容器的一些机制,在读取配置文件进行数据库的配置等等是很有必要的,所以我们要考虑配置文件的的读取方式以及各个方式的实用性 2.配置文件的读取方式我这里介绍2种,目的是掌握这2种就可以 ...

  4. Spring中注入List,Set,Map,Properties的xml文件配置方法

    下面的例子展示了如何注入 List – <list/> Set – <set/> Map – <map/> Properties – <props/> ...

  5. Struts2 资源配置文件国际化

    Struts2 资源配置文件国际化 Struts2资源文件的命名规范:basename_language_country.properties Struts2国际化如果系统同时存在资源文件.类文件,系 ...

  6. SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池

    三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...

  7. spring读取xml配置文件(二)

    一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...

  8. SpringBoot读取外部配置文件的方法

    SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...

  9. Spring使用外部的配置文件

    在使用Spring做web项目的时候,通常会使用到数据库的连接信息 jdbcUrl driverClass username password 那么应该如何使用这些属性呢? 如在Spring中使用数据 ...

随机推荐

  1. HttpWebRequest 高效并发问题

    默认请求连接数 是2,在服务器操作系统上默认为10. 如果不修改这个并发连接限制,那么客户端同时可以建立的 http 连接数就只有2个或10个. System.Net.ServicePointMana ...

  2. [字符串][NOIP2012]Vigenère密码

    Vigenère密码 题目描述 16世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère密码的加密解密算法简单易用,且破译难度比较高, ...

  3. springmvc mybatis shiro ios android构建cms系统

    开发语言: java.ios.android 部署平台: linux.window jdk版本:JDK1.7以上版本 开发工具: eclipse.idea等 服务器中间件:Tomcat 6.7.Jbo ...

  4. P1613 跑路(倍增 + floyd)

    https://www.luogu.org/problemnew/show/P1613 思路: 1.读入 2.建图 3.对于每一个点,向距离它 2^k 长度的点连一条长度为 1 的边 4.在新图上跑1 ...

  5. LOJ-10108(欧拉回路+并查集)一个图至少用几笔画成

    题目链接:传送门 思路: 用并查集统计出每个区块奇数个节点的个数x,每个区块对笔画的贡献是max(x/2,1): 然后每个区块求和即可. #include<iostream> #inclu ...

  6. ABP框架系列之三十四:(Multi-Tenancy-多租户)

    What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...

  7. PLL与PHY的连接:通道绑定或者不绑定

    用到的术语: clock skew的产生 延时与时钟线的长度及被时钟线驱动的时序单元的负载电容.个数有关 由于时钟线长度及负载不同,导致时钟信号到达相邻两个时序单元的时间不同 于是产生所谓的clock ...

  8. js-闪烁的文字

    <!DOCTYPE html><html>    <head lang="en">        <meta charset=" ...

  9. Django View 进阶

    返回404 from django.http import HttpResponse, HttpResponseNotFound def not_found(request): ) 或 return ...

  10. 【WPF】实现类似QQ聊天消息的界面

    最近公司有个项目,是要求实现类似 QQ 聊天这种功能的. 如下图 这没啥难的,稍微复杂的也就表情的解析而已. 表情在传输过程中的实现参考了新浪微博,采用半角中括号代表表情的方式.例如:“abc[dog ...