From f1bb8001fef51afd1605d809273b51f481cc662b Mon Sep 17 00:00:00 2001
From: youys <1272586223@qq.com>
Date: Wed, 17 Aug 2022 11:41:39 +0800
Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BE=AA=E7=8E=AF=E4=BE=9D?=
=?UTF-8?q?=E8=B5=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 1 +
.../java/net/educoder/config/CacheConfig.java | 28 +++++++++++--------
.../educoder/controller/CloudController.java | 14 ++++++----
3 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/pom.xml b/pom.xml
index 212195e..bb7e61a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,6 +28,7 @@
5.7.11
+
org.projectlombok
lombok
diff --git a/src/main/java/net/educoder/config/CacheConfig.java b/src/main/java/net/educoder/config/CacheConfig.java
index d4078c5..cea73ea 100644
--- a/src/main/java/net/educoder/config/CacheConfig.java
+++ b/src/main/java/net/educoder/config/CacheConfig.java
@@ -5,11 +5,12 @@ import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import net.educoder.constant.CommonConstants;
import net.educoder.util.JCloudUtil;
+import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;
/**
@@ -19,24 +20,16 @@ import java.util.concurrent.TimeUnit;
*/
@Slf4j
@Configuration
-public class CacheConfig {
+public class CacheConfig implements BeanPostProcessor {
- @Value("${jcloud.password}")
+ @Value("${jcloud.username}")
private String username;
@Value("${jcloud.password}")
private String password;
- @PostConstruct
- public void init() {
- Cache cache = guavaCache();
- String apiToken = JCloudUtil.getApiToken(username, password);
- log.info("token init--------{}", apiToken);
- cache.put(CommonConstants.API_TOKEN, apiToken);
- }
-
- @Bean
+ @Bean("guavaCache")
public Cache guavaCache() {
return CacheBuilder.newBuilder()
@@ -49,4 +42,15 @@ public class CacheConfig {
})
.build();
}
+
+ @Override
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+ if("guavaCache".equals(beanName)){
+ Cache cache = (Cache)bean;
+ String apiToken = JCloudUtil.getApiToken(username, password);
+ log.info("token init--------{}", apiToken);
+ cache.put(CommonConstants.API_TOKEN, apiToken);
+ }
+ return bean;
+ }
}
diff --git a/src/main/java/net/educoder/controller/CloudController.java b/src/main/java/net/educoder/controller/CloudController.java
index 9952b77..ee07ec0 100644
--- a/src/main/java/net/educoder/controller/CloudController.java
+++ b/src/main/java/net/educoder/controller/CloudController.java
@@ -7,6 +7,7 @@ import net.educoder.util.JCloudUtil;
import net.educoder.util.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -23,14 +24,15 @@ import org.springframework.web.bind.annotation.RestController;
public class CloudController {
- @Value("${jcloud.password}")
+ @Value("${jcloud.username}")
private String username;
@Value("${jcloud.password}")
private String password;
@Autowired
- private Cache cache;
+ @Qualifier("guavaCache")
+ private Cache guavaCache;
/**
@@ -41,7 +43,7 @@ public class CloudController {
@PostMapping("/createCloudHost")
public ResponseResult createCloudHost() {
- String apiToken = cache.getIfPresent(CommonConstants.API_TOKEN);
+ String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN);
if (StringUtils.isBlank(apiToken)) {
apiToken = refreshApiToken();
}
@@ -58,7 +60,7 @@ public class CloudController {
@PostMapping("/getCloudHostList")
public ResponseResult getCloudHostList() {
- String apiToken = cache.getIfPresent(CommonConstants.API_TOKEN);
+ String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN);
if (StringUtils.isBlank(apiToken)) {
apiToken = refreshApiToken();
}
@@ -76,7 +78,7 @@ public class CloudController {
@PostMapping("/resetCloudHost")
public ResponseResult resetCloudHost(String serverId) {
- String apiToken = cache.getIfPresent(CommonConstants.API_TOKEN);
+ String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN);
if (StringUtils.isBlank(apiToken)) {
apiToken = refreshApiToken();
}
@@ -104,7 +106,7 @@ public class CloudController {
private String refreshApiToken() {
String apiToken = JCloudUtil.getApiToken(username, password);
log.info("token refresh--------{}", apiToken);
- cache.put(CommonConstants.API_TOKEN, apiToken);
+ guavaCache.put(CommonConstants.API_TOKEN, apiToken);
return apiToken;
}