Spring @ Value uses the operation to get the configuration information as null

  • 2021-10-24 23:08:16
  • OfStack

1. Project background

1. Introduction:

Recently, in the springboot project, it is necessary to do a Alibaba Cloud OSS picture upload function point, and extract the OSS picture upload code into the public tool class for the convenience of other modules.

2. Introduction to the problem

The OSS parameter defined in the yml file will not be obtained when using some custom variable information of OSS1 in the public tool class.

3. Problem analysis

After analysis, it is determined that the common tool class method is not managed as bean by the spring container, so the configuration file information cannot be obtained. Previously, we used to get profile information through @ Value at controller layer and service layer, which is managed by spring container as bean, so it can be easily obtained.

4. Problem solving

Create a new parameter fetch class and decorate the class with the @ Component annotation.

See the following code:

Parameter initialization:

@Component
public class AliyunOssConstants implements InitializingBean{ 
 /**
  *  Alibaba Cloud OSS Regional node 
  */
 @Value("${aliyunOss.file.endpoint}")
    private String endpoint;
 
 /**
  *  Alibaba Cloud OSSaccessKeyId
  */
    @Value("${aliyunOss.file.keyid}")
    private String accessKeyId;
 
    /**
     *  Alibaba Cloud OSSaccessKeySecret
     */
    @Value("${aliyunOss.file.keysecret}")
    private String accessKeySecret;
 
    /**
     *  Alibaba Cloud OSSbucket Name 
     */
    @Value("${aliyunOss.file.bg.bucketname}")
    private String bg_bucketname;
 
    /**
     *  Alibaba Cloud OSSBucket Domain name 
     */
    @Value("${aliyunOss.file.filehost}")
    private String filehost;    
    public  static  String SPRING_FILE_ENDPOINT;
    public  static  String SPRING_FILE_ACCESS_KEY_ID;
    public  static  String SPRING_FILE_ACCESS_KEY_SECRET;
    public  static  String SPRING_FILE_BG_BUCKET_NAME;
    public  static  String SPRING_FILE_FILE_HOST;
 
 @Override
 public void afterPropertiesSet() throws Exception {
  SPRING_FILE_ENDPOINT = endpoint;
  SPRING_FILE_ACCESS_KEY_ID = accessKeyId;
  SPRING_FILE_ACCESS_KEY_SECRET = accessKeySecret;
  SPRING_FILE_BG_BUCKET_NAME = bg_bucketname;
  SPRING_FILE_FILE_HOST = filehost;
 } 
}
Use parameters:

@Slf4j
public class AliyunOSSUtil { 
 private static String endpoint=AliyunOssConstants.SPRING_FILE_ENDPOINT;
 private static String accessKeyId=AliyunOssConstants.SPRING_FILE_ACCESS_KEY_ID;
 private static String accessKeySecret=AliyunOssConstants.SPRING_FILE_ACCESS_KEY_SECRET;
 private static String bucketname=AliyunOssConstants.SPRING_FILE_BG_BUCKET_NAME;
 private static String filehost=AliyunOssConstants.SPRING_FILE_FILE_HOST; 
    //  Alibaba Cloud OSS Upload file method 
}

@ Value Why you can't get a value

In springboot, the general method to get the value in the configuration file is


@Value("${tag}")
private String tagValue;

However, when taking the value, sometimes this tagvalue is NULL, and the possible reasons are:

1. Class without @ Component (or @ service, etc.)


@Component // Omission 
class TestValue{
    @Value("${tag}")
    private String tagValue;
}

2. Class is instantiated by new instead of @ Autowired


@Component 
class TestValue{
    @Value("${tag}")
    private String tagValue;
} 
class Test{
    ...
    TestValue testValue = new TestValue()
Correct way:

1. Use @ Autowired injection

2. Injection value in controller layer


Related articles: