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稳定版本作为重新梳理系列,希望对看本文的你有所帮助 ...
随机推荐
- Angular 行内式依赖注入
var app = angular.module('myApp', ['ng']); //创建一个自定义服务app.factory('$Debug', function () { return { d ...
- Java微信二次开发(九)
多媒体文件上传与下载 第一步:找到包com.wtz.vo,新建类WeixinMedia.java package com.wtz.vo; /** * @author wangtianze QQ:864 ...
- delphi 通过事务插入数据
orsn1.StartTransaction; try qry1.Sql.Clear; qry1.Sql.Text:=' select * from log '; qry1.Open; qry1.In ...
- html 文档类型
<!doctype>用来声明html的版本,浏览器只有知道html的版本后才能正确显示文档,<!DOCTYPE>本身不是一个标签,而是一个声明.
- nginx-日志统计
#!/bin/bash fd=/tmp/log# pv 点击量echo "###################点击量 --$pv-- #########################&q ...
- Cookie 类
最近项目中用到cookie,所以就写了个cookie帮助类 public class CookHelper { HttpResponseBase Response = null; HttpReques ...
- LOJ6387 [THUPC2018] 绿绿与串串 【manacher】
题目分析: 比较简单,先跑一边manacher,然后对于回文部分可以碰到末尾的一定满足条件,否则向后转移. 代码: #include<bits/stdc++.h> using namesp ...
- BZOJ1002 [FJOI2007] 轮状病毒 【递推】
题目分析: 推基尔霍夫矩阵后可以发现递推式 代码: n = input() f0 = 1 f1 = 5 f3 = 0 if n == 1: print f0 elif n == 2: print f1 ...
- jquery 取id模糊查询
[属性名称] 匹配包含给定属性的元素[att=value] 匹配包含给定属性的元素 (大小写区分)[att*=value] 模糊匹配[att!=value] 不能是这个值[att$=value] 结尾 ...
- 恕我直言,在座的各位根本写不好Java!
其实,本不想把标题写的那么恐怖,只是发现很多人干了几年 Java 以后,都自认为是一个不错的 Java 程序员了,可以拿着上万的工资都处宣扬自己了,写这篇文章的目的并不是嘲讽和我一样做 Java 的同 ...