@Value(“${xxxx}”)
@Value("${xxxx}")
是 Spring 框架中一个常见的用法,
用于,将配置属性(通常是在application.properties
或 application.yml
文件中定义的属性)的值,注入到Spring管理的,Bean的字段、方法参数、setter方法中,
这里的 ${xxxx}
是一个占位符,它会在Spring容器启动时,被替换为 xxxx
对应的实际值。
需要注意的是
当使用 @Value("${xxxx}") 时,
如果 xxxx 在配置文件中不存在,那么默认行为是注入 null(对于对象类型)或抛出异常(对于基本类型和包装类型的原始值)。
为了避免这种情况,你可以提供一个默认值,如 @Value("${my.property:defaultValue}"),
这样当 my.property 不存在时,会注入 defaultValue。
需要注意的是
在Spring应用程序中,Spring框架本身并不会自动加载配置文件,
但是,
在Spring Boot应用程序中,默认情况下会自动加载application.properties
或application.yml
配置文件中的属性值,
因为,Spring Boot提供了自动配置的功能,会在应用程序启动时加载这些默认的配置文件,
但是,
如果需要加载其他配置文件,可以使用@PropertySource
注解来指定要加载的配置文件,
例如:
@PropertySource("classpath:custom.properties")
这样就可以加载custom.properties
文件中的属性值供应用程序使用,
如果不使用@PropertySource
注解指定其他配置文件,Spring Boot会默认加载application.properties
或application.yml
文件中的属性值。
-------------------------------------------------再举举举举举个例子------------------------------------------
@PropertySource
注解 和 @Value
注解,可以实现,从指定的配置文件中读取属性值并注入到Spring组件中,
例子:
custom.properties
配置文件,内容如下:
my.name=张三
然后创建一个Spring组件类MyConfig
,使用@PropertySource
注解指定要加载的配置文件,并使用@Value
注解读取属性值:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:custom.properties")
public class MyConfig {
@Value("${my.name}")
private String name;
public String getName() {
return name;
}
}
这里,@PropertySource("classpath:custom.properties")
,指定了要加载的配置文件为custom.properties
,
然后,通过@Value("${my.name}")
读取my.name
属性的值,并注入到name
属性中,
这样就可以实现,从指定的配置文件中读取属性值,并注入到Spring组件中。
直接赋值
@Component
public class MyComponent {
@Value("张三")
private String name;
public String getName() {
return name;
}
}
注入到字段
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
@Value("${my.property}")
private String myProperty;
// ... 其他方法 ...
}
1、
在 @Value("${my.property}") 这个注解中,
"my.property" 并不是指一个名为 my 的配置文件的 property 属性,
实际上,my.property 是指 Spring 配置文件(通常是 application.properties 或 application.yml)中的一个键(key)。
2、
当 Spring 容器加载应用上下文时,它会读取这些配置文件,并将其中的键值对存储起来,
然后,
当 Spring 容器创建 MyComponent 的实例时,它会查找名为 my.property 的配置值,并将其注入到 myProperty 字段中。
所以,如果您在 application.properties 文件中有以下内容:
my.property=SomeValue
那么,Spring 容器在创建 MyComponent 的实例时,会将 myProperty 字段的值设置为 SomeValue。
3、
这里并不涉及任何名为 my 的配置文件,
my.property 只是一个配置属性的键名,
它可以在任何 Spring 支持的配置文件中定义,包括 application.properties、application.yml 或通过其他配置源(如环境变量、命令行参数等)定义。
4、最好,确保,您的 application.properties 或 application.yml 文件位于 Spring Boot 项目的 src/main/resources 目录下
注入到构造器参数
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
private final String myProperty;
public MyComponent(@Value("${my.property}") String myProperty) {
this.myProperty = myProperty;
}
// ... 其他方法 ...
}
这里的 @Value("${my.property}") String myProperty 表示,将配置文件中定义的my.property属性的值,注入到MyComponent类的构造函数中的myProperty参数中
注入到setter方法
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
private String myProperty;
@Value("${my.property}")
public void setMyProperty(String myProperty) {
this.myProperty = myProperty;
}
}
这里的 @Value("${my.property}"),表示,将配置文件中定义的my.property属性的值,注入到setMyProperty方法的参数myProperty中,
Spring在初始化"MyComponent bean"时,会自动调用这个方法,并将对应的属性值传入
注入到方法返回值
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Value("${my.property}")
@Bean
public String myProperty() {
return "The value of my.property is: " + this.myProperty;
}
}
在这个例子中,myProperty()方法,会返回一个字符串,其中包含 my.property 的值。
@Value(“#{xxxx}”)
@Value("#{xxxx}")
表示,使用Spring Expression Language (SpEL)来解析和计算xxxx这个表达式。
SpEL是一种强大的表达式语言,
它支持查询和操作对象图、执行方法调用和访问JavaBean属性等,
在`@Value`注解中使用`#{...}`语法,可以执行更复杂的操作,比如:调用bean的方法、引用其他的bean等。
举个例子
有一个bean定义了一个方法返回一个字符串,如下:
@Component
public class MyComponent {
public String getMessage() {
return "Hello, World!";
}
}
可以在另一个bean中使用SpEL来调用这个方法并注入其结果:
@Component
public class AnotherComponent {
@Value("#{myComponent.getMessage()}")
private String message;
// ... 其他方法 ...
}
在这个例子中,"#{myComponent.getMessage()}" 是一个SpEL表达式,
它调用了"MyComponent bean" 的 getMessage()方法,并将返回的结果,注入到AnotherComponent的message属性中。