02、@PropertySource指定配置文件的属性映射到JavaBean属性
零、@PropertySource 功能类似于
<context:property-placeholder location="classpath*:/config/load.properties"/>
@Configuration
@PropertySources(
@PropertySource(value = "classpath:/config/load.properties",ignoreResourceNotFound = true,encoding = "UTF-8")
)
public class ServerProperties {
@Value("${liubin.test.name}")
private String name; public String getName() {
return name;
}
}
一、引入配置文件注解PropertySource源码
package org.springframework.context.annotation;
import ....;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
String name() default "";
String[] value();//可以是多个
boolean ignoreResourceNotFound() default false;
String encoding() default "";
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
二、PropertySources中包含多个PropertySource
package org.springframework.context.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {
PropertySource[] value();
}
三、引入配置文件后配置项被@Value注解使用
@Component
@PropertySource("classpath:teacher.properties")
public class Student { @Value("${name}")
private String name; @Value("${age}")
private int age; @Value("${sex}")
private String sex; @Value("${height}")
private int height; @Value("${weight}")
private int weight;
}
四、引入配置文件后配置项被Spring Boot 的@ConfigurationProperties使用,一站式注入到相应的JavaBean的所有属性中
@PropertySource({"classpath:teacher.properties"}) //指定某配置文件,无此参数则在默认全局配置文件中获取
@Component
@ConfigurationProperties(prifix=“teacher”) //配置文件中前缀名为teacher的配置项的所有属性都映射到此JavaBean中
public class Teacher {
private String name;
private int age;
private String sex;
private int height;
private int weight;
get\set方法
}
五、不同的属性配置文件中若有相同的配置项,写在后面配置文件中的属性覆盖前面的
@PropertySources(
{
@PropertySource("classpath:teacher.properties")
}
)
02、@PropertySource指定配置文件的属性映射到JavaBean属性的更多相关文章
- AutoMapper在MVC中的运用04-string映射各种类型、一个属性映射多个属性等
本篇AutoMapper使用场景: ※ 类型转换,源string类型分别转换成int, DateTime,Type ※ 源和目标都包含复杂类型属性 ※ 把源中的一个属性映射到目标中的多个属性 类型转换 ...
- Hibernate持久化类属性映射
Hibernate充当应用程序和数据库之间的中间件,实现二者之间的交互操作,他对JDBC进行了封装,以完全面向对象的方式来操作数据. 适用于有多个数据源的情况下,不必去考虑不同数据源的操作差异. Hi ...
- EFCodeFirst属性映射约定
EFCodeFirst属性映射约定 EFCodeFirst 属性映射约定 CodeFirst与数据表之间得映射方式又两种:Data Annotation和Fluent API 默认约定: 表名为类名的 ...
- Spring注解@PropertySource加载配置文件和SpringBoot注解@Value、@ConfigurationProperties进行属性映射
SpringBoot的配置文件 位置:resources目录下 配置文件的作用: (1).SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用a ...
- MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射
在上一章中我们学习了<MyBatis学习总结(一)——ORM概要与MyBatis快速起步>,这一章主要是介绍MyBatis核心配置文件.使用接口+XML实现完整数据访问.输入参数映射与输出 ...
- Entity Framework Code First属性映射约定
Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...
- graph-tool文档(一)- 快速开始使用Graph-tool - 2.属性映射、图的IO和Price网络
目录: 属性映射 -- 内部属性映射 图的I/O 构建一个 Price网络(例) 名词解释: Property maps:属性映射 PropertyMap:一个类 scalar value types ...
- Touch 方法&属性 映射工具
Touch 方法&属性 映射工具(0.5 版本) 标签 : github 线上后门与接口调试: 原先需要测试一个接口(如Dubbo.DAO), 或为线上留后门, 需要写大量的Web层(Api. ...
- EntityFramework Core 2.1重新梳理系列属性映射(一)
前言 满血复活啦,大概有三个月的时间没更新博客了,关于EF Core最新进展这三个月也没怎么去看,不知现阶段有何变化没,本文将以EF Core 2.1稳定版本作为重新梳理系列,希望对看本文的你有所帮助 ...
随机推荐
- 配置自己的Maven方式并使用Maven 运行项目Idea的maven的项目
(1) 当安装了 maven之后,需要导入项目代码,然后编译执行: 打开Idea ==>然后点击小扳手==>在搜索框中输入maven==>然后找到 Maven home direct ...
- Luogu5055 【模板】可持久化文艺平衡树(fhq-treap)
注意下传标记时也需要新建节点.空间开的尽量大. #include<iostream> #include<cstdio> #include<cmath> #inclu ...
- python 时间模块 -- time
time 时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该先导入模块. # 常用方法 import time print("现在执行我") time.sleep( ...
- Network of Schools POJ - 1236(强连通+缩点)
题目大意 有N个学校,这些学校之间用一些单向边连接,若学校A连接到学校B(B不一定连接到A),那么给学校A发一套软件,则学校B也可以获得.现给出学校之间的连接关系,求出至少给几个学校分发软件,才能使得 ...
- Minimum Cost POJ - 2516 (模板题 spfa最小费用最大流)
题意: 人回家,一步一块钱,有x个人,y个房子,求能回家的最大人数且使之费用最小 解析: 就是....套模板,,,, 建图(⊙﹏⊙)...要仔细观察呐 对于人拆不拆都可以 都能过,,,,这里贴上拆开 ...
- 网络流Dinic模板
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...
- emwin之窗口关闭按钮用法
@2018-07-27 [小记] 使用函数 FRAMEWIN_AddCloseButton() 实现关闭当前窗口的功能时,调用其窗口的父窗口必须处于打开状态,否则将导致假死(当前窗口死了,系统还在工作 ...
- 单片机I/O口的结构的详解
1.什么是源型 漏型?什么是上拉电阻?下拉电阻?什么是 线驱动输出 集电极开路输出,推挽式输出? 我们先来说说集电极开路输出的结构.集电极开路输出的结构如图1所示,右边的那个三极管集电极什么都不接,所 ...
- time_t和difftime
在C++中,下面这段代码可以获取一段时间差. time_t t1 = time(NULL); Sleep(); time_t t2 = time(NULL); cout << diffti ...
- [luogu1655][小朋友的球]
luogu1665 思路 一道第二类斯特兰数的模板题.只不过需要写个高精. f[i][j]表示前i个球放到j个盒子里的方案数.第i个球可以单独一个盒子,所以f[i][j]+=f[i-1][j-1].还 ...