Spring中的@Value和@PropertySource注解详解

 更新时间:2023年11月04日 11:10:36   作者:大树下躲雨  
这篇文章主要介绍了Spring中的@Value和@PropertySource注解详解,@PropertySource:读取外部配置文件中的key-value保存到运行的环境变量中,本文提供了部分实现代码,需要的朋友可以参考下

 一、@Value和@PropertySource

1、@Value

@Value注解:为属性赋值

赋值方式:

  • 基本数值
  • SpEl表达式 #{}
  • ${},读取配置文件[xxx.properties]中的值,配合注解@PropertySource使用
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
   /**
    * The actual value expression such as <code>#{systemProperties.myProp}</code>
    * or property placeholder such as <code>${my.app.myProp}</code>.
    */
   String value();
}

2、@PropertySource

@PropertySource:读取外部配置文件中的key-value保存到运行的环境变量中

@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;
}

二、@Value和@PropertySource案例

1、项目结构

在这里插入图片描述

2、Person

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Person {
    /**
     * @Value赋值方式1:基本数值
     */
    @Value("张三")
    private String name;
    /**
     * @Value赋值方式2:SpEl表达式  #{}
     */
    @Value("#{ 20-2 }")
    private int age;
    /**
     * @Value赋值方式3:${},读取配置文件[xxx.properties]中的值
     */
    @Value("${person.phone}")
    private String phone;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                '}';
    }
}

3、配置类

import org.springframework.context.annotation.*;

/**
 * 使用@PropertySource注解可以读取外部配置文件中的key-value保存到运行的环境变量中
 */
@PropertySource(value = {"classpath:/person.properties"},encoding = "UTF-8")
@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {
}

4、外部文件person.properties

person.phone = 11111111

5、测试类

import com.dashu.bean.Person;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class Main {
    public static void main(String[] args) {
        //加载配置类获取容器
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        //容器中获取Person
        Person person = annotationConfigApplicationContext.getBean(Person.class);
        //打印
        System.out.println(person);
        System.out.println("--------------------");
        /**
         * 获取运行时环境实例
         */
        ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment();
        /**
         * 根据外部文件的key,从环境中获取value
         */
        String property = environment.getProperty("person.phone");
        //打印
        System.out.println(property);
    }
}

6、测试结果

在这里插入图片描述

到此这篇关于Spring中的@Value和@PropertySource注解详解的文章就介绍到这了,更多相关@Value和@PropertySource注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JAVA springboot如何开启俩端口

    JAVA springboot如何开启俩端口

    这篇文章主要介绍了JAVA springboot如何开启俩端口问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • Java由浅入深带你精通继承super

    Java由浅入深带你精通继承super

    继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为
    2022-03-03
  • Java实现abc字符串排列组合

    Java实现abc字符串排列组合

    这篇文章主要为大家详细介绍了JAVA实现abc字符串的排列组合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • 详谈Java中net.sf.json包关于JSON与对象互转的坑

    详谈Java中net.sf.json包关于JSON与对象互转的坑

    下面小编就为大家分享一篇Java中net.sf.json包关于JSON与对象互转的坑,具有很好的参考价值,希望对大家有所帮助
    2017-12-12
  • Java多线程Semaphore工具的使用详解

    Java多线程Semaphore工具的使用详解

    Semaphore 是一种用于控制线程并发访问数的同步工具。它通过维护一定数量的许可证来限制对共享资源的访问,许可证的数量就是可以同时访问共享资源的线程数目,需要的朋友可以参考下
    2023-05-05
  • 详解Spring boot Admin 使用eureka监控服务

    详解Spring boot Admin 使用eureka监控服务

    本篇文章主要介绍了详解Spring boot Admin 使用eureka监控服务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • 详解maven的install的作用

    详解maven的install的作用

    这篇文章主要介绍了详解maven的install的作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • SpringBoot整合Thymeleaf与FreeMarker视图层技术

    SpringBoot整合Thymeleaf与FreeMarker视图层技术

    在目前的企业级应用开发中,前后端分离是趋势,但是视图层技术还占有一席之地。Spring Boot 对视图层技术提供了很好的支持,福安防推荐使用的模板引擎是Thymeleaf,不过想FreeMarker也支持,JSP技术在这里并不推荐使用
    2022-08-08
  • 如何修改覆盖spring boot默认日志策略logback详解

    如何修改覆盖spring boot默认日志策略logback详解

    这篇文章主要给大家介绍了关于如何修改覆盖spring boot默认日志策略logback的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • Java实现8种排序算法的示例代码

    Java实现8种排序算法的示例代码

    这篇文章主要介绍了8种JAVA实现排序算法的示例代码,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06

最新评论