步骤 1 : 先运行,看到效果,再学习 步骤 2 : 模仿和排错 步骤 3 : 问题 步骤 4 : SpringContextUtil 步骤 5 : IndexService 步骤 6 : IndexController 步骤 7 : 测试
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,379个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,379个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,379个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,379个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.trend.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil implements ApplicationContextAware {
private SpringContextUtil() {
System.out.println("SpringContextUtil()");
}
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext){
System.out.println("applicationContext:"+applicationContext);
SpringContextUtil.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,379个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.trend.service;
import cn.how2j.trend.pojo.Index;
import cn.how2j.trend.util.SpringContextUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
@CacheConfig(cacheNames="indexes")
public class IndexService {
private List<Index> indexes;
@Autowired RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "third_part_not_connected")
public List<Index> fresh() {
indexes =fetch_indexes_from_third_part();
IndexService indexService = SpringContextUtil.getBean(IndexService.class);
indexService.remove();
return indexService.store();
}
@CacheEvict(allEntries=true)
public void remove(){
}
@Cacheable(key="'all_codes'")
public List<Index> store(){
System.out.println(this);
return indexes;
}
@Cacheable(key="'all_codes'")
public List<Index> get(){
return CollUtil.toList();
}
public List<Index> fetch_indexes_from_third_part(){
List<Map> temp= restTemplate.getForObject("http://127.0.0.1:8090/indexes/codes.json",List.class);
return map2Index(temp);
}
private List<Index> map2Index(List<Map> temp) {
List<Index> indexes = new ArrayList<>();
for (Map map : temp) {
String code = map.get("code").toString();
String name = map.get("name").toString();
Index index= new Index();
index.setCode(code);
index.setName(name);
indexes.add(index);
}
return indexes;
}
public List<Index> third_part_not_connected(){
System.out.println("third_part_not_connected()");
Index index= new Index();
index.setCode("000000");
index.setName("无效指数代码");
return CollectionUtil.toList(index);
}
}
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,379个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.trend.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.how2j.trend.pojo.Index;
import cn.how2j.trend.service.IndexService;
@RestController
public class IndexController {
@Autowired IndexService indexService;
// http://127.0.0.1:8001/freshCodes
// http://127.0.0.1:8001/getCodes
// http://127.0.0.1:8001/removeCodes
@GetMapping("/freshCodes")
public List<Index> fresh() throws Exception {
return indexService.fresh();
}
@GetMapping("/getCodes")
public List<Index> get() throws Exception {
return indexService.get();
}
@GetMapping("/removeCodes")
public String remove() throws Exception {
indexService.remove();
return "remove codes successfully";
}
}
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,379个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|