SpringPipelineFactory.java
1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.geccocrawler.gecco.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.pipeline.PipelineFactory;
import com.geccocrawler.gecco.spider.SpiderBean;
@Service
public class SpringPipelineFactory implements PipelineFactory, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public Pipeline<? extends SpiderBean> getPipeline(String name) {
try {
Object bean = applicationContext.getBean(name);
if(bean instanceof Pipeline) {
return (Pipeline<? extends SpiderBean>)bean;
}
} catch(NoSuchBeanDefinitionException ex) {
System.out.println("no such pipeline : " + name);
}
return null;
}
}