properties-property value acquisition

更新时间:
复制 MD 格式

This tutorial uses the SOFABoot Core project as an example to demonstrate how to obtain the property information configured in the resources/config/application.properties. Other types of SOFABoot projects also adopt similar methods and will not be described in detail.

Precondition

  • Click Download Sample Project.

  • Use the IDE to open the folder where the main pom.xml file is located. For more information about how to open a project in an IDE, see SOFABoot Quick Start.

    Note

    A SOFABoot project that does not introduce any middleware is a standard Spring Boot project. For more information about Spring Boot, see here.

Obtain properties configuration information

After the project prototype is generated by using the mvn command, you can perform the following operations to obtain the properties of the properties configuration:

  • Use the @ Value annotation to load the corresponding configuration property.

  • Obtain configuration properties by injecting.

Use the @ Value annotation to load configuration attributes

For a property value in a application.properties, if a class needs to obtain the corresponding value, perform the following steps:

  1. Configure the class as a bean.

  2. You can use @Value("${attribute key}") to inject a property value.

The code logic. Example:

@Component
public class AppConfig{

   @Value("${app.config.first}")
   private String appConfigFirst;

    public String getAnnotationAppConfigFirst(){
        return appConfigFirst;
    }
}

2 ways to configure a class as a bean

Take the above AppConfig as an example:

  • method a: add a @Component annotation to the class to be configured.

    • SOFABoot is based on Spring boot and does not need to declare the package scan tag <context:component-scan base-package="com.alipay.APPNAME">.

    • In the startup function SpringBootSpringApplication of SOFABoot, an annotation @org.springframework.boot.autoconfigure.SpringBootApplication has been added. You can use this annotation to automatically configure the package scan path.

  • the procedure for method b is as follows:

    1. Delete the @Component annotation.

    2. Configure this bean in the configuration file of the project, such as the src/main/resources/META-INF/SOFABoot_coreDemo/SOFABoot_coreDemo-service.xml under the SOFABoot_coreDemo-service module. Example:

      !" -- Inject sofa-config through @ Value -->
      <bean id="appConfig" class="com.alipay.APPNAME.configuration.AppConfig"/>

Verify the configuration information

The procedure is as follows:

  1. In the application.properties, configure the key-value pair: app.config.first=FirstConfig.

  2. Start the class src/main/java/com/alipay/APPNAME/SOFABootSpringApplication.java under the SOFABoot_coreDemo-service module and use the sample code to achieve the following goals:

    • Obtains the configuration information.

    • Output the obtained information to the console.

    • You can use TestCase to compare the expected information with the actual information.

The code logic. Example:

//configed in application.properties
final String expectedValue ="FirstConfig";
//get AppConfig
AppConfig appConfig = applicationContext.getBean(AppConfig.class);


//get value by annnotation
// Obtain the configuration.
String annotationValue = appConfig.getAnnotationAppConfigFirst();
// Console output
System.out.println("FirstAppConfigByAnnotation = "+ annotationValue);
// Test by using TestCase:
TestCase.assertEquals(expectedValue,annotationValue);

For more information, see Notes @ SpringBootApplication and Spring Boot code structure best practices.

Obtain the property values of the properties configuration by injecting beans

To obtain the property values of the properties configuration by injecting org.springframework.core.env.Environment types of beans, perform the following steps:

  1. Configure the class as a bean. For more information, see How to configure a class as a bean.

  2. Inject Environment objects.

  3. Get the property value through the getProperty() method.

The code logic. Example:

@Component
public class AppConfig{
   @Autowired
    private Environment environment;

    public String getEnvFirstConfigValue(){
         return environment.getProperty("app.config.first");
    }
}

Verify the configuration information

The procedure is as follows:

  1. In the application.properties, configure the key-value pair: app.config.first=FirstConfig.

  2. Start the class src/main/java/com/alipay/APPNAME/SOFABootSpringApplication.java under the SOFABoot_coreDemo-service module and use the sample code to achieve the following goals:

    • Obtains the configuration information.

    • Output the obtained information to the console.

    • You can use TestCase to compare the expected information with the actual information.

The code logic for obtaining configuration information. Example:

//configed in application.properties
final String expectedValue ="FirstConfig";
//get AppConfig
AppConfig appConfig = applicationContext.getBean(AppConfig.class);


//get value by environment
String envValue = appConfig.getEnvFirstConfigValue();
// The properties obtained from the console output.
System.out.println("FirstAppConfigByEnvironment = "+ envValue);
// Use TestCase to test.
TestCase.assertEquals(envValue,annotationValue);