Spring @Value

@Value injection

The case of not passing the injected properties of the configuration file

Dynamically inject external values ​​into the Bean through @Value, the use cases are as follows:

Inject ordinary strings,
inject operating system properties,
inject expression results,
inject other bean properties: inject the properties of beanInject objects, another
inject file resources, and
inject URL resources

See the detailed code:

@Value("normal")
    private String normal; // 注入普通字符串

    @Value("#{systemProperties['os.name']}")
    private String systemPropertiesName; // 注入操作系统属性

    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private double randomNumber; //注入表达式结果

    @Value("#{beanInject.another}")
    private String fromAnotherBean; // 注入其他Bean属性:注入beanInject对象的属性another,类具体定义见下面

    @Value("classpath:com/hry/spring/configinject/config.txt")
    private Resource resourceFile; // 注入文件资源

    @Value("http://www.baidu.com")
    private Resource testUrl; // 注入URL资源

Inject other bean properties: inject the properties of the beanInject object another

@Component
public class BeanInject {
    @Value("其他Bean的属性")
    private String another;

    public String getAnother() {
        return another;
    }

    public void setAnother(String another) {
        this.another = another;
    }
}

Injecting properties through the configuration file

The value of the external configuration file is dynamically injected into the Bean through @Value. There are two main types of configuration files:

  • application.properties. application.properties loads this file by default when spring boot starts
  • Custom properties file. The custom property file is loaded via @PropertySource. @PropertySource can load multiple files at the same time or a single file. If the same key exists in the same first property file and the second property file, the key in the last property file is activated. The path of the loaded file can also be configured with variables, as follows: ${anotherfile.configinject}, this value is defined in the first property file config.properties
    The content of the first property file config.properties is as follows:
    ${anotherfile.configinject} as the second Variable value of the loading path of a property file
book.name=bookName
anotherfile.configinject=placeholder

The content of the second properties file config_placeholder.properties is as follows:

book.name.placeholder=bookNamePlaceholder

The following uses @Value("${app.name}") grammar to inject the value of the property file into the bean property value. See the detailed code:

@Component
// 引入外部配置文件组:${app.configinject}的值来自config.properties。
// 如果相同
@PropertySource({"classpath:com/hry/spring/configinject/config.properties",
    "classpath:com/hry/spring/configinject/config_${anotherfile.configinject}.properties"})
public class ConfigurationFileInject{
    @Value("${app.name}")
    private String appName; // 这里的值来自application.properties,spring boot启动时默认加载此文件

    @Value("${book.name}")
    private String bookName; // 注入第一个配置外部文件属性

    @Value("${book.name.placeholder}")
    private String bookNamePlaceholder; // 注入第二个配置外部文件属性

    @Autowired
    private Environment env;  // 注入环境变量对象,存储注入的属性值

    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("bookName=").append(bookName).append("\r\n")
        .append("bookNamePlaceholder=").append(bookNamePlaceholder).append("\r\n")
        .append("appName=").append(appName).append("\r\n")
        .append("env=").append(env).append("\r\n")
        // 从eniroment中获取属性值
        .append("env=").append(env.getProperty("book.name.placeholder")).append("\r\n");
        return sb.toString();
    }   
}

Source: https://blog.csdn.net/hry2015/article/details/72353994

Guess you like

Origin blog.csdn.net/qianzhitu/article/details/107655407