目录
这个在某些业务场景 无法实现了
static执行顺序高 , 被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
- @Slf4j
- @Component
- public class OSSUtils {
-
-
- @Value("${spring.cloud.alicloud.access-key}")
- public String accessKeyIdConst;
-
- @Value("${spring.cloud.alicloud.secret-key}")
- public String accessKeySecretConst;
-
- @Value("${spring.cloud.alicloud.oss.endpoint}")
- public String endpointConst;
-
- @Value("${spring.cloud.alicloud.oss.bucket}")
- public String bucketNameConst;
-
- @Value("${spring.alibaba.oss.callBack.ip}")
- public String ossCallBackIpConst;
-
- public static String accessKeyId;
- public static String accessKeySecret;
- public static String endpoint;
- public static String bucketName;
- public static String ossCallBackIp;
-
- @PostConstruct
- public void init() {
- accessKeyId = accessKeyIdConst;
- accessKeySecret = accessKeySecretConst;
- endpoint = endpointConst;
- bucketName = bucketNameConst;
- ossCallBackIp=ossCallBackIpConst;
- }
- }
- @SpringBootTest
- public class TestOssConfig {
-
- @Test
- void testOss01(){
- System.out.println(OSSUtils.accessKeyId);
- System.out.println(OSSUtils.accessKeySecret);
- System.out.println(OSSUtils.bucketName);
- System.out.println(OSSUtils.endpoint);
-
- }
-
- }
测试有效
对构造函数进行注入(变量前面)@Value("${spring.alibaba.oss.callBack.ip}"