浅析PropertySource 基本使用

简介: org.springframework.context.annotation.PropertySource 是一个注解,可以标记在类上、接口上、枚举上,在运行时起作用。而@Repeatable(value = PropertySources.class) 表示在PropertySources 中此注解时可以重复使用的。

一、PropertySource 简介二、@PropertySource与Environment读取配置文件三、@PropertySource与@Value读取配置文件@Value 基本使用@Value 高级用法四、@PropertySource 与 @Import

一、PropertySource 简介

org.springframework.context.annotation.PropertySource 是一个注解,可以标记在类上、接口上、枚举上,在运行时起作用。而@Repeatable(value = PropertySources.class) 表示在PropertySources 中此注解时可以重复使用的。如下:

32.jpg

二、@PropertySource与Environment读取配置文件

此注解@PropertySource 为Spring 中的 Environment提供方便和声明机制,通常与Configuration一起搭配使用。

  • 新建一个maven 项目,添加pom.xml 依赖:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.spring.propertysource</groupId>
    <artifactId>spring-propertysource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-propertysource</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring.version>4.3.13.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

一般把版本名称统一定义在 标签中,便于统一管理,如上可以通过${…} 来获取指定版本。


  • 定义一个application.properties 来写入如下配置


com.spring.name=liuXuan

com.spring.age=18


  • 新建一个TestBean,定义如下属性


public class TestBean {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "TestBean{" +
"name='" + name + '\&#39;' +
", age=" + age +
'}';
}
}


  • 新建一个main class ,用来演示@PropertySource 的使用


@Configuration
@PropertySource(value = "classpath:application.properties",ignoreResourceNotFound = false)
public class SpringPropertysourceApplication {
@Resource
Environment environment;
@Bean
public TestBean testBean(){
TestBean testBean = new TestBean();
// 读取application.properties中的name
testBean.setName(environment.getProperty("com.spring.name"));
// 读取application.properties中的age
testBean.setAge(Integer.valueOf(environment.getProperty("com.spring.age")));
System.out.println("testBean = " + testBean);
return testBean;
}
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringPropertysourceApplication.class);
TestBean testBean = (TestBean)applicationContext.getBean("testBean");
}
}

输出:

testBean = TestBean{name='liuXuan', age=18}

Refreshing the spring context

@Configuration : 相当于 标签,注意不是,一个配置类可以有多个bean,但是只能有一个

@PropertySource: 用于引入外部属性配置,和Environment 配合一起使用。其中ignoreResourceNotFound 表示没有找到文件是否会报错,默认为false,就是会报错,一般开发情况应该使用默认值,设置为true相当于生吞异常,增加排查问题的复杂性。

引入PropertySource,注入Environment,然后就能用environment 获取配置文件中的value值。

三、@PropertySource与@Value读取配置文件

@Value 基本使用

我们以DB的配置文件为例,来看一下如何使用@Value读取配置文件

  • 首先新建一个DBConnection,具体代码如下:


// 组件bean
@Component
@PropertySource("classpath:db.properties")
public class DBConnection {
@Value("${DB_DRIVER_CLASS}")
private String driverClass;
@Value("${DB_URL}")
private String dbUrl;
@Value("${DB_USERNAME}")
private String userName;
@Value("${DB_PASSWORD}")
private String password;
public DBConnection(){}
public void printDBConfigs(){
System.out.println("Db Driver Class = " + driverClass);
System.out.println("Db url = " + dbUrl);
System.out.println("Db username = " + userName);
System.out.println("Db password = " + password);
}
}

类上加入@Component 表示这是一个组件bean,需要被spring进行管理,@PropertySource 用于获取类路径下的db.properties 配置文件,@Value用于获取properties中的key 对应的value值,printDBConfigs方法打印出来对应的值。

  • 新建一个db.properties,具体文件如下


#MYSQL Database Configurations
DB_DRIVER_CLASS=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://localhost:3306/test
DB_USERNAME=cxuan
DB_PASSWORD=111111
APP_NAME=PropertySourceExample

这是一个MYSQL连接数据库驱动的配置文件。


  • 新建一个SpringMainClass,用于测试DBConection中是否能够获取到@Value的值


public class SpringMainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 注解扫描,和@ComponentScan 和 基于XML的配置<context:component-scan base-package>相同
context.scan("com.spring.propertysource.app");
// 刷新上下文环境
context.refresh();
System.out.println("Refreshing the spring context");
// 获取DBConnection这个Bean,调用其中的方法
DBConnection dbConnection = context.getBean(DBConnection.class);
dbConnection.printDBConfigs();
// 关闭容器(可以省略,容器可以自动关闭)
context.close();
}
}

输出:

Refreshing the spring context

Db Driver Class = com.mysql.jdbc.Driver

Db url = jdbc:mysql://localhost:3306/test

Db username = cxuan

Db password = 111111

@Value 高级用法

在实现了上述的例子之后,我们再来看一下@Value 的高级用法:

  • @Value 可以直接为字段赋值,例如:


@Value("cxuan")
String name;
@Value(10)
Integer age;
@Value("${APP_NAME_NOT_FOUND:Default}")
private String defaultAppName;


  • @Value 可以直接获取系统属性,例如:


@Value("${java.home}")
// @Value("#{systemProperties['java.home']}") SPEL 表达式
String javaHome;
@Value("${HOME}")
String dir;


  • @Value 可以注解在方法和参数上


@Value("Test") // 可以直接使用Test 进行单元测试
public void printValues(String s, @Value("another variable") String v) {
...
}

修改DBConnection后的代码如下:

public class DBConnection {
@Value("${DB_DRIVER_CLASS}")
private String driverClass;
@Value("${DB_URL}")
private String dbUrl;
@Value("${DB_USERNAME}")
private String userName;
@Value("${DB_PASSWORD}")
private String password;
public DBConnection(){}
public void printDBConfigs(){
System.out.println("Db Driver Class = " + driverClass);
System.out.println("Db url = " + dbUrl);
System.out.println("Db username = " + userName);
System.out.println("Db password = " + password);
}
}

在com.spring.propertysource.app 下 新增DBConfiguration,作用是配置管理类,管理DBConnection,并读取配置文件,代码如下:

@Configuration
@PropertySources({
@PropertySource("classpath:db.properties"),
@PropertySource(value = "classpath:root.properties", ignoreResourceNotFound = true)
})
public class DBConfiguration {
@Value("Default DBConfiguration")
private String defaultName;
@Value("true")
private boolean defaultBoolean;
@Value("10")
private int defaultInt;
@Value("${APP_NAME_NOT_FOUND:Default}")
private String defaultAppName;
@Value("#{systemProperties['java.home']}")
// @Value("${java.home}")
private String javaHome;
@Value("${HOME}")
private String homeDir;
@Bean
public DBConnection getDBConnection() {
DBConnection dbConnection = new DBConnection();
return dbConnection;
}
@Value("Test") // 开启测试
public void printValues(String s, @Value("another variable") String v) {
System.out.println("Input Argument 1 = " + s);
System.out.println("Input Argument 2 = " + v);
System.out.println("Home Directory = " + homeDir);
System.out.println("Default Configuration Name = " + defaultName);
System.out.println("Default App Name = " + defaultAppName);
System.out.println("Java Home = " + javaHome);
System.out.println("Home dir = " + homeDir);
System.out.println("Boolean = " + defaultBoolean);
System.out.println("Int = " + defaultInt);
}
}

使用SpringMainClass 进行测试,测试结果如下:

Input Argument 1 = Test

Input Argument 2 = another variable

Home Directory = /Users/mr.l

Default Configuration Name = Default DBConfiguration

Default App Name = Default

Java Home = /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre

Home dir = /Users/mr.l

Boolean = true

Int = 10

Refreshing the spring context

Db Driver Class = com.mysql.jdbc.Driver

Db url = jdbc:mysql://localhost:3306/test

Db username = cxuan

Db password = 111111

可以看到上述代码并没有显示调用printValues 方法,默认是以单元测试的方式进行的。

四、@PropertySource 与 @Import

@Import 可以用来导入 @PropertySource 标注的类,具体代码如下:

  • 新建一个PropertySourceReadApplication 类,用于读取配置文件并测试,具体代码如下:
// 导入BasicPropertyWithJavaConfig类
@Import(BasicPropertyWithJavaConfig.class)
public class PropertySourceReadApplication {
@Resource
private Environment env;
@Value("${com.spring.name}")
private String name;
@Bean("context")
public PropertySourceReadApplication contextLoadInitialized(){
// 用environment 读取配置文件
System.out.println(env.getProperty("com.spring.age"));
// 用@Value 读取配置文件
System.out.println("name = " + name);
return null;
}
public static void main(String[] args) {
// AnnotationConnfigApplicationContext 内部会注册Bean
new AnnotationConfigApplicationContext(PropertySourceReadApplication.class);
}
}
  • 新建一个BasicPropertyWithJavaConfig 类,用于配置类并加载配置文件
@Configuration
@PropertySource(value = "classpath:application.properties")
public class BasicPropertyWithJavaConfig {
public BasicPropertyWithJavaConfig(){
super();
}
}

启动PropertySourceReadApplication ,console能够发现读取到配置文件中的value值

18

name = cxuan

欢迎关注 Java建设者

            </div>
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
安全 Windows
WindowsXP现在还能使用吗
WindowsXP现在还能使用吗
986 2
|
新零售 人工智能
阿里巴巴联合汉仪重磅推出五款人工智能字体:汉仪天真体、英雄体等
众所周知传统做字的人力成本非常之高,如果全靠人类设计师来完成,一套标准字库从设计到完成需要一年多的时间。
13502 0
|
机器人 开发工具 Web App开发
干货满满!解密阿里云RPA (机器人流程自动化)的产品架构和商业化发展
阿里云RPA,作为阿里云自研8年的技术,在资本的热捧下,逐渐从幕后来到台前,成为企业服务市场的黑马。本文将从产品上全面剖析,阿里云RPA这款产品的现阶段情况,同时简单谈谈阿里云RPA的商业化进展。
8414 0
干货满满!解密阿里云RPA (机器人流程自动化)的产品架构和商业化发展
|
12月前
|
机器学习/深度学习 人工智能 自然语言处理
《人工智能知识图谱构建与应用的最新突破与成果》
在人工智能蓬勃发展的背景下,知识图谱的构建与应用成为热点。新技术如基于大语言模型和向量库的方法,提升了实体识别、关系抽取及图谱优化的效率和精度。这些创新已在医疗、电力、信息检索等领域取得显著成效,如思通数科平台使病例处理速度提升40%,国网湖北电力提高信息检索准确性。未来,知识图谱将更高效、智能地处理多模态数据,并在金融、教育等更多领域发挥重要作用,但也需关注数据隐私和安全问题。
529 9
|
算法 数据建模 网络安全
阿里云SSL证书2024双11优惠,WoSign DV证书220元/年起
2024阿里云11.11金秋云创季火热进行中,活动月期间(2024年11月01日至11月30日),阿里云SSL证书限时优惠,部分证书产品新老同享75折起;通过优惠折扣、叠加满减优惠券等多种方式,阿里云WoSign SSL证书将实现优惠价格新低,DV SSL证书220元/年起。
951 5
|
人工智能 自然语言处理 运维
干货|AI赋能教学开发-利用AI生成教案、课件和讲义
本文分享了高校教师利用AI工具设计课程方案和课件的经验,分为两部分。第一部分详细介绍使用GPT4o生成高质量课程大纲的过程,包括客户需求分析、提示词设计及优化调整。第二部分展示如何借助AIPPT快速制作精美课件,并介绍AIPPT的长文档解读和链接生成PPT等功能。此外,文章还分享了多个实用的AI工具、智能体和提示词技巧,助力提升教学效率与质量。
2624 3
|
弹性计算 监控 安全
阿里云APP有什么用?在哪里可以下载纯净版?阿里云APP下载及使用介绍
手机阿里云App是阿里云官方出品的移动应用,为用户提供随时随地触达阿里云的能力。目前网上有很多第三方平台都可以下载,但是有的用户担心第三方平台的APP不是纯净版,可能带有其他插件。其实我们可以通过阿里云官方就能下载到纯净版的APP,纯净版APP具有安全、便捷、快速、实时的特点,帮助用户在手机上快速购买续费、进行云产品的管控。
1833 0
阿里云APP有什么用?在哪里可以下载纯净版?阿里云APP下载及使用介绍
|
存储 缓存 安全
阿里云服务器2核4G、4核8G、8核16G配置活动价格及实例规格选择参考(2024更新)
阿里云服务器2核4G、4核8G、8核16G配置有三十几种实例规格可选,2024年,经济型e、通用算力型u1、计算型c7和计算型c8y实例2核4G、4核8G、8核16G配置的云服务器有优惠,其中,通用算力型u1实例2核4G,5M固定带宽,80G ESSD Entry盘企业用户购买只要199元/1年,另外轻量应用服务器2核4G配置也有优惠,目前价格只要165元/1年。下面是2024年截至目前阿里云服务器2核4G、4核8G、8核16G配置最新活动价格及选择建议参考。
阿里云服务器2核4G、4核8G、8核16G配置活动价格及实例规格选择参考(2024更新)
|
存储 弹性计算 对象存储
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
|
移动开发 芯片 内存技术
经典蓝牙架构分层及协议总览
经典蓝牙架构分层及协议总览
2565 0