SpringBoot:详解依赖注入和使用配置文件
    		       		warning:
    		            这篇文章距离上次修改已过427天,其中的内容可能已经有所变动。
    		        
        		                
                
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class MyComponent {
 
    // 使用@Value注解注入简单类型
    @Value("${my.simpleValue}")
    private String simpleValue;
 
    // 使用@Value注解注入配置文件中的字符串数组
    @Value("${my.arrayValue}")
    private String[] arrayValue;
 
    // 使用@Value注解注入配置文件中的字符串列表
    @Value("${my.listValue}")
    private List<String> listValue;
 
    // 使用@Value注解注入配置文件中的嵌套属性
    @Value("${my.nested.property}")
    private String nestedProperty;
 
    // 获取方法,供其他组件调用
    public String getSimpleValue() {
        return simpleValue;
    }
 
    // ... 其他getter方法
}这段代码展示了如何在SpringBoot应用中使用@Value注解来注入配置文件中的属性值。通过这种方式,我们可以将配置文件中的参数直接注入到Java类的字段中,使得配置参数的管理和使用更加方便和集中。
评论已关闭