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中net.sf.json包关于JSON与对象互转的坑
下面小编就为大家分享一篇Java中net.sf.json包关于JSON与对象互转的坑,具有很好的参考价值,希望对大家有所帮助2017-12-12
详解Spring boot Admin 使用eureka监控服务
本篇文章主要介绍了详解Spring boot Admin 使用eureka监控服务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12
SpringBoot整合Thymeleaf与FreeMarker视图层技术
在目前的企业级应用开发中,前后端分离是趋势,但是视图层技术还占有一席之地。Spring Boot 对视图层技术提供了很好的支持,福安防推荐使用的模板引擎是Thymeleaf,不过想FreeMarker也支持,JSP技术在这里并不推荐使用2022-08-08
如何修改覆盖spring boot默认日志策略logback详解
这篇文章主要给大家介绍了关于如何修改覆盖spring boot默认日志策略logback的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2018-10-10


最新评论