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.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值