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; }