how2j.cn

下载区
文件名 文件大小
请先登录 347k
增值内容 347k
347k

6分25秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器

步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : 问题   
步骤 4 : SpringContextUtil   
步骤 5 : IndexService   
步骤 6 : IndexController   
步骤 7 : 测试   

步骤 1 :

先运行,看到效果,再学习

edit
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,378个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,378个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,378个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
步骤 4 :

SpringContextUtil

edit
增值内容,请先登录
完整的 SpringCloud 趋势量化投资项目,使用 Springboot 、Vue.js、redis, Zipkin, RabbitMQ, SpringCloud 等一整套技术栈, 从无到有涵盖全部59个知识点,378个开发步骤, 充实 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个知识点,378个开发步骤, 充实 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个知识点,378个开发步骤, 充实 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个知识点,378个开发步骤, 充实 SpringCloud 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2022-03-09 注解@Cacheable和@CachePut
我爱学习学习使我快乐




这里保存缓存数据不是应该用这个注解吗@CachePut,为什么用@Cacheable,我测试了一下用这两个注解效果好像一样。。。不知道为什么。。
加载中

							

							


1 个答案

how2j
答案时间:2022-03-13
可以百度一下@CachePut 和 @Cacheable之间的区别。 他们在不同的场景下有不同的用处哦



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2021-07-05 SpringContextUtil 和自己用new建立的对象有什么不同么?
Humbot




如题,应该是自己基础不太好,那位好心可以提醒一下
    public List<Index> fresh() {
        indexes =fetch_indexes_from_third_part();
        IndexService indexService =  SpringContextUtil.getBean(IndexService.class);
        //IndexService indexService = new IndexService();
        indexService.remove();
        return indexService.store();
    }

							


2 个答案

luzhi0324
答案时间:2021-07-28
确保Spring不会失效

how2j
答案时间:2021-07-08
SpringContextUtil.getBean 方式拿到的对象,会注入配置中对应的对象。 而new 出来就不会了。



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2021-01-27 站长,我要提供一种便捷的方法( ̄︶ ̄)↗ 
2020-10-30 刷新机制
2020-08-12 关于从redis获取数据


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 11 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 实践项目-趋势投资SpringCloud-采集和储存微服务-刷新 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图