@Value configuration and use of the Spring and SpringBoot

Copyright: code word is not easy, please indicate the source ~~ https://blog.csdn.net/m0_37190495/article/details/83615399
Reproduced, please indicate the source:
Original starting in: http://www.zhangruibin.com
article from RebornChang's blog

Shamelessly advertising, blogger personal blog address Portal , Welcome

@Value comment Profile

In the development process, a lot of things we have to place it in the configuration file, then use the code to parse the configuration file, so the system is easy to maintain, reduce operation and maintenance costs. The traditional wording reads the configuration file is:
`` ``

Properties properties = new Properties();
// 使用InPutStream流读取properties文件
BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/config.properties"));
properties.load(bufferedReader);
// 获取key对应的value值
properties.getProperty(String key); 
````

Spring simplifies this step while using @Value form of custom annotations for reading the contents of the file object, the source code for:



        //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //

    package org.springframework.beans.factory.annotation;

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Value {
        String value();
    }

And its use is very simple:

    1、@Value("#{configProperties['key']}")

    2、@Value("${key}")

This method greatly simplifies project configuration, increase the flexibility of business.

In Spring arrangement and method of use

Conducted in spring bean configuration profile

Spring configuration file written

<--配置一个名字叫做configProperties的bean,这个bean的位置(locations)是classpath:/config/test.properties-->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
     <property name="locations">  
         <list>  
             <value>classpath:/config/test.properties</value>  
         </list>  
     </property>  
 </bean>  

Write /config/test.properties

requestUrl=testUrlFromProperties

In application.yml file Springboot in configuration and usage

Application.yml provided as a name and a parameter testPropertyParam1 testPropertyBean objects, a method is provided as shown below:
Code Screenshot

In Springboot of application.properties file to configure and use

Application.properties file anywhere in the top grid added:

aaaa="这里的信息是从文件中读取到的"
bbbb="safdsfsgdsfhsdfhsdfhshsdhsdf"

Notes injection method 1

After such a configuration can be used to annotate files @Value value, the value of the method is as follows:

     @Value("#{configProperties['requestUrl']}") 
     private String setRequestUrl;

Notes injection method 2

  @Value("${bbbb}")
   private String aaaa ;

note:

1. The injection @Value annotation only parameter out the method;

2. Set the parameter value is UTF-8 characters and other characters, it will be garbled.

Guess you like

Origin blog.csdn.net/m0_37190495/article/details/83615399