日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot WebMagic 入库时 mapper注入提示空指针,以及正确的操作

發(fā)布時間:2024/4/19 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot WebMagic 入库时 mapper注入提示空指针,以及正确的操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本屌研究了一整天

網(wǎng)上說 實現(xiàn)Pipeline接口中的 process是多線程的,所以注入后不是同一個對象,無所報空指針

貼出代碼

1.啟動類

@SpringBootApplication @EnableScheduling//開啟定時任務(wù) @MapperScan(basePackages = {"com.xianbaovip.project"}) public class ProjectApplication {public static void main(String[] args) {SpringApplication.run(ProjectApplication.class, args);} }

重點是@EnableScheduling注解,

2.WebMagic 的爬取 PageProcessor

@Component public class WebMagicSpider1 implements PageProcessor {private Site site = Site.me().setRetryTimes(3).setSleepTime(100);@Overridepublic Site getSite() {return site;}public void process(Page page) {this.saveMessage(page);}//WebMagic的入庫方法@Autowiredprivate GatherPipeline gatherPipeline; private void saveMessage(Page page){page.putField("Content", page.getHtml().xpath("//div[@class='comment-body']/div/p[1]").all());page.putField("Source", constant.reptileSource1);}//initialDelay 延遲多久執(zhí)行第一次任務(wù)//fixedDelay 相隔多久執(zhí)行上一次任務(wù)@Scheduled(initialDelay = 1000,fixedDelay = 100*1000)public void process() {Spider.create(new WebMagicSpider1()).addUrl("http://www.xianbaoi.vip").addPipeline(this.gatherPipeline).thread(1).run();} }

重點

1.@Component注解
2.@Scheduled定時任務(wù)

2.WebMagic 的爬取 Pipeline

@Component public class GatherPipeline implements Pipeline {//自己封裝的入庫方法@Autowiredprivate GatherMapper gatherMapper;@Overridepublic void process(ResultItems resultItems, Task task) {for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {if (entry.getKey().contains("Content")) {List<String> value = (List<String>)entry.getValue();//取值入庫for (String Content : value) {Gather gather = new Gather();gather.setGatherContent(Content);gather.setGatherSource(sourceString);gather.setGatherTitle("test");gather.setCreateTime(new Date());this.gatherMapper.insert(gather);}}}}}

重點

1.調(diào)用mapper的時候 使用this.

總結(jié)

以上是生活随笔為你收集整理的Spring Boot WebMagic 入库时 mapper注入提示空指针,以及正确的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。