Spring Boot WebMagic 入庫時 mapper注入提示空指針,以及正確的操作

本屌研究了一整天

網上說 實現Pipeline接口中的 process是多線程的,所以注入後不是同一個對象,無所報空指針

貼出代碼

1.啓動類

@SpringBootApplication
@EnableScheduling//開啓定時任務
@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);
    @Override
    public Site getSite() {
        return site;
    }

    public void process(Page page) {
          this.saveMessage(page);
    }

	//WebMagic的入庫方法
    @Autowired
    private  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 延遲多久執行第一次任務
	//fixedDelay 相隔多久執行上一次任務
    @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定時任務

2.WebMagic 的爬取 Pipeline

@Component
public class GatherPipeline implements Pipeline {
	//自己封裝的入庫方法
    @Autowired
    private GatherMapper gatherMapper;
    @Override
    public 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.調用mapper的時候 使用this.

發佈了55 篇原創文章 · 獲贊 16 · 訪問量 8366
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章