Read an array from a configuration file using the @ Value annotation

  • 2021-10-16 01:56:37
  • OfStack

The @ Value annotation reads an array from a configuration file

Function: Take value from configuration file

Usage:

1. Take a single value

(1) configuration. properties configuration


status.notice.switch=open

(2) Automatic injection of java files


@Value("${status.notice.switch}")
private String statusNoticeSwitch;

2. Fetch the array

(1) configuration. properties configuration


lanwon.hospital.id=43534,234543,w353654

(2) Automatic injection of java files


@Value("#{'${lanwon.hospital.id}'.split(',')}")
private List<String> hospitalIdList;

Inject values using @ Value annotations (configuration file read)

By using @ Value annotation in Spring component, configuration information can be obtained directly from. properties,. yum and other configuration files, which is convenient for realizing the configuration operation of the project.

1. Configuration

1.1 Use

1. @ Value ("${key}")

2. @ Value ("# {configProperties ['key']}") (SpEL expression)

1.2 Default Value Configuration

1. Basic mode: ${key}: defaultvalue

2. SpEL mode:

Set the default value using Spring Expression Language (SpEL).

The following code is marked in the systemProperties properties file. If the value of some. key is not set, my default system property value is set to the default value.


@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;

2. Use scenarios

2.1 Declared variables


public static class FieldValueTestBean {
  @Value("#{ systemProperties['user.region'] }")
 private String defaultLocale;
}

2.2 setter method


public static class PropertyValueTestBean {
    private String defaultLocale;
    @Value("#{ systemProperties['user.region'] }")
    public void setDefaultLocale(String defaultLocale) {
        this.defaultLocale = defaultLocale;
    } 
}

2.3 Methods


public class SimpleMovieLister {
    private MovieFinder movieFinder;
    private String defaultLocale;
    @Autowired
    public void configure(MovieFinder movieFinder,
            @Value("#{ systemProperties['user.region'] }") String defaultLocale) {
        this.movieFinder = movieFinder;
        this.defaultLocale = defaultLocale;
    }
    // ...
}

2.4 Construction Methods


public class MovieRecommender { 
    private String defaultLocale; 
    private CustomerPreferenceDao customerPreferenceDao; 
    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
            @Value("#{systemProperties['user.country']}") String defaultLocale) {
        this.customerPreferenceDao = customerPreferenceDao;
        this.defaultLocale = defaultLocale;
    } 
    // ...
}

3 Injection of various attributes and their default value settings

3.1 String

Property of type string sets the default value.


@Value("${some.key:my default value}")
private String stringWithDefaultValue;

some. key has no value set, the stringWithDefaultValue variable value will be set to my default value.

If the default value is set to null, it will also be set to the default value.


@Value("${status.notice.switch}")
private String statusNoticeSwitch;
0

3.2 Basic Types

Base type sets the default value.


@Value("${status.notice.switch}")
private String statusNoticeSwitch;
1

@Value("${status.notice.switch}")
private String statusNoticeSwitch;
2

3.3 Packaging type

The wrapper type sets the default value.


@Value("${some.key:true}")
private Boolean booleanWithDefaultValue;
 
@Value("${some.key:42}")
private Integer intWithDefaultValue;

3.4 Array

The default value of the array can be divided by commas.


@Value("${status.notice.switch}")
private String statusNoticeSwitch;
4

Related articles: