How to solve the problem that spring @Value gets null value

Many times, we will use Spring's EL expressions to obtain configuration information from property files through the @Value("${xxx.xxx}") annotation. However, if you don't pay attention, the obtained value is null.
Today Let's talk about the points that should be paid attention to when using Spring @Value.

Problem Description

The following code obtains the value of the application.name property from the application.properties file through @Value("${xxx.xxx}")

# application.properties
application.name = yuan
public class PropertyBean {
    
    
    @Value("${application.name}")
    private String applicationName;

    public static void main(String[] args) {
    
    
        System.out.println("applicationName=" + applicationName);
    }
}

Let's first look at the results of the operation:

applicationName=

Very strange, why did not get the value of application.name?

Wrong usage scenario

Misuse scenario one

The class is not handed over to spring management, the following code:

public class PropertyBean {
    
    
    @Value("${application.name}")
    private String applicationName;
}

@Value("${}") is Spring's EL expression, which is an injection method of spring,
so if you want sprig to inject the value of application.name into the applicationName field of the PropertyBean class, you must hand over the PropertyBean class to Spring Management, so that spring knows where it needs to be injected automatically.
Therefore, the solution can be to increase the @Component annotation on the PropertyBean class:

@Component
public class PropertyBean {
    
    
    @Value("${application.name}")
    private String applicationName;
}

Misuse Scenario 2

The fields of the class are modified by static or final, as shown in the following code:

@Component
public class PropertyBean {
    
    
    @Value("${application.name}")
    private static String applicationName;

}

The applicationName field of the PropertyBean class is modified by static, resulting in null. This is because static variables are attributes of the class, not attributes of the object, and Spring performs dependency injection based on the attributes of the object. So injecting static variables with the @Value annotation fails.
The solution is to remove the static or final in front of the field applicationName.

Misuse Scenario 3

The place where PropertyBean is used is new instead of dependency injection, and the obtained value is null. The following code:

@Component
public class PropertyBean {
    
    
    @Value("${application.name}")
    private String applicationName;

    // 此处 applicationName有值
    public static void main(String[] args) {
    
    
        System.out.println("applicationName=" + applicationName);
    }
}

@Service
public class SpringElService {
    
    
    public String getApplicationName(){
    
    
        // 通过new PropertyBean()获取对象
        PropertyBean bean = new PropertyBean();
        // 此处 applicationName为null
        return bean.getApplicationName();
    }
}

This scenario is most likely to be overlooked. The applicationName in the PropertyBean class has a value, but it is null in the SpringElService class. Why?
In order to help understand, I drew a simple abstract diagram. In scene three, there are two types of beans in PropertyBean, one is the PropertyBean bean managed in the Spring container, and the value spring of applicationName can be injected automatically, and the other is manually created. PropertyBean bean, applicationName of String type, if no assignment is displayed, it is empty by default.
img.png

The correct way to use the code is as follows:

# aplication.properties
application.name = yuan
@Component
public class PropertyBean {
    
    
    @Value("${application.name}")
    private String applicationName;
}

@Service
public class SpringElService {
    
    
    
    private final PropertyBean bean;
    // 构造器注入 bean
    public SpringElService(PropertyBean bean){
    
    
        this.bean = bean;
    }

    public String getApplicationName(){
    
    
        return bean.getApplicationName();
    }
}

Summarize

  • This article analyzes the reasons why @Value gets empty values ​​through several common error scenarios
  • Spring provides a lot of convenience for development, but a little carelessness may cause exceptions, so for a new framework, it is necessary to understand the principle, as the saying goes, you must know why.

More spring questions

Guess you like

Origin blog.csdn.net/m0_54369189/article/details/126656663