解决循环依赖

master
youys 2 years ago
parent d5fe4abd47
commit f1bb8001fe

@ -28,6 +28,7 @@
<version>5.7.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

@ -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<String, String> cache = guavaCache();
String apiToken = JCloudUtil.getApiToken(username, password);
log.info("token init--------{}", apiToken);
cache.put(CommonConstants.API_TOKEN, apiToken);
}
@Bean
@Bean("guavaCache")
public Cache<String, String> 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<String, String> cache = (Cache)bean;
String apiToken = JCloudUtil.getApiToken(username, password);
log.info("token init--------{}", apiToken);
cache.put(CommonConstants.API_TOKEN, apiToken);
}
return bean;
}
}

@ -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<String, String> cache;
@Qualifier("guavaCache")
private Cache<String, String> guavaCache;
/**
@ -41,7 +43,7 @@ public class CloudController {
@PostMapping("/createCloudHost")
public ResponseResult<String> 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<String> 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;
}

Loading…
Cancel
Save