1.实现InitializingBean接口,重写afterPropertiesSet方法,将@Value赋值给成员变量的属性赋值给静态变量,示例如下:
/** * @Classname FileUtil * @Description TODO * @Date 2021/8/5 14:21 * @Created by cc */ @Component @PropertySource(value = "classpath:fdfs_client.conf") public class FileUtil implements InitializingBean { private static String protocol; private static String trackerNginxAddr; private static String separator; @Value("${fdfs.protocol}") private String getProtocol; @Value("${fdfs.tracker_nginx_addr}") private String getTrackerNginxAddr; @Value("${fdfs.separator}") private String getSeparator; @Override public void afterPropertiesSet() { protocol = getProtocol; trackerNginxAddr = getTrackerNginxAddr; separator = getSeparator; } }
2.然后下面直接使用静态变量即可