在工具类中引用注解service

4 篇文章 0 订阅
2 篇文章 0 订阅

今天在写爬虫时遇到了在工具类中不能注解成功的问题。下面我把我的解决办法共享给大家。需要引入一个工具类。

package com.nieyb.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author chenhy
 *以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	@Override
	/**
	* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
	*/
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextHolder.applicationContext = applicationContext;
	}

	/**
	* 取得存储在静态变量中的ApplicationContext.
	*/
	public static ApplicationContext getApplicationContext() {
		checkApplicationContext();
		return applicationContext;
	}

	/**
	* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
	*/
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		checkApplicationContext();
		return (T) applicationContext.getBean(name);
	}

	/**
	* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
	*/
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<T> clazz) {
		checkApplicationContext();
		return (T) applicationContext.getBeansOfType(clazz);
	}

	/**
	* 清除applicationContext静态变量.
	*/
	public static void cleanApplicationContext() {
		applicationContext = null;
	}

	private static void checkApplicationContext() {
		if (applicationContext == null) {
			throw new IllegalStateException("applicaitonContext未注入,请在springMVC.xml中定义SpringContextHolder");
		}
	}

}

在启动程序入口的man方法中添加注解

package com.nieyb;

import com.nieyb.dao.ArticleMapper;
import com.nieyb.dao.RuleMapper;
import com.nieyb.model.Article;
import com.nieyb.model.Rule;
import com.nieyb.service.ArticleService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Hello world!
 *
 */
@SpringBootApplication(scanBasePackages = {"com.nieyb"})
@RestController
@EnableScheduling
@MapperScan("com.nieyb.dao")
@EnableFeignClients(basePackages = {"com.nieyb.api"})
public class App 
{
   
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class,args);
    }
}

然后在工具类顶部添加

@Component注解。用
ArticleService articleService = SpringContextHolder.getBean("ArticleServiceImpl");来引入注解但是要在你要引入的service的实现类里面添加
@Service("ArticleServiceImpl")注解。参数一定要一致。

我完整的工具类是这样的:

package com.nieyb.gecco;


import com.alibaba.fastjson.JSONObject;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.JsonPipeline;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.request.HttpRequest;
import com.nieyb.model.Article;
import com.nieyb.model.ArticleDTO;
import com.nieyb.model.Dictionary;
import com.nieyb.service.ArticleService;
import com.nieyb.service.DictionaryService;
import com.nieyb.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@PipelineName(value = "newsContentPipelines")
public class NewsCountentPopeline extends JsonPipeline {

    @Override
    public void process(JSONObject newsContent) {
        HttpRequest currRequest = HttpGetRequest.fromJson(newsContent.getJSONObject("request"));
        String title = newsContent.getString("title");
        String form = newsContent.getString("form");
        String news = newsContent.getString("news");
        if (title != "" && title != null && form != "" && form != null && news != "" && news != null) {
            System.out.println(title+"*****"+form);
            ArticleDTO articleDTO = new ArticleDTO();
            Article article = new Article();
            article.setContent(news);
            article.setForm(form);

            article.setTitle(title);

            articleDTO.setContentBody(news);
            articleDTO.setArticleOrigin(form);
            articleDTO.setArticleTitle(title);
            articleDTO.setContentTitle(title);
            articleDTO.setArticleStatus(1);
            //System.out.println(article);

            save(article,articleDTO);

        }


    }

    /***
     * 将文章入库
     * */


    public void save(Article article,ArticleDTO articleDTO){
        ArticleService articleService = SpringContextHolder.getBean("ArticleServiceImpl");
        DictionaryService dictionaryService = SpringContextHolder.getBean("DictionaryService");
        Dictionary dictionary = dictionaryService.selectById(1);
        String culmn =dictionary.getValue();
        articleDTO.setChannelId(culmn);
        article.setClumnId(culmn);
        //articleService.saveCMS(articleDTO);//保存的CMS库
        //articleService.saveCache(article);//保存到中间缓存库中
        articleService.addArticle(article);//保存到本地库
    }


}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用Spring框架的开发,需要在Eclipse安装Spring插件。下面是在Eclipse使用Spring框架的步骤: 1. 在Eclipse安装Spring插件 打开Eclipse,选择菜单栏的Help -> Eclipse Marketplace,在搜索框输入“Spring Tools”,选择Spring Tools 4进行安装。 2. 创建Spring项目 在Eclipse选择File -> New -> Other,选择Spring -> Spring Project,然后选择Spring项目类型,例如Spring MVC Project。 3. 配置Spring环境 在Spring项目,需要配置Spring的配置文件,例如applicationContext.xml。在项目创建该文件,并进行配置。 4. 编写Spring代码 在项目编写Spring代码,可以使用注解或XML配置方式,例如使用注解配置Spring Bean: ```java @Component public class MyBean { // ... } ``` 5. 运行Spring项目 在Eclipse选择Spring项目,右键选择Run As -> Spring Boot App,即可运行Spring项目。 以上就是在Eclipse使用Spring框架的基本步骤,希望对你有所帮助。 ### 回答2: Spring是一个开源的轻量级Java框架,广泛应用于企业级应用开发。在Eclipse使用Spring可以方便地进行Spring项目的开发和管理。 首先,我们需要在Eclipse安装Spring插件。可以通过Eclipse Marketplace或者手动安装插件的方式将Spring插件集成到Eclipse。安装完成后,我们可以在Eclipse的导航栏看到Spring的相关选项。 使用Spring的第一步是创建一个Spring项目。在Eclipse,我们可以通过选择"New" -> "Other" -> "Spring Legacy Project"来创建一个Spring项目。在创建项目的过程,可以选择使用不同的Spring版本,如Spring MVC、Spring Boot等。 创建项目后,我们可以在项目的配置文件进行Spring的配置。在配置文件,我们可以定义Spring的Bean,并指定依赖注入、切面、事务等相关的内容。使用Eclipse提供的代码提示功能,可以方便地完成配置文件的编写。 在编写Java代码时,我们可以使用Eclipse提供的Spring插件来简化Spring的使用。例如,可以通过插件提供的代码生成功能快速生成DAO、Service、Controller等Spring组件的代码。同时,插件还提供了Spring的注解快速生成和跳转等功能,方便我们在代码使用Spring的特性。 在调试和测试Spring项目时,Eclipse也提供了一些方便的工具。我们可以使用Eclipse的调试功能来定位和解决Spring项目的问题。同时,通过配置JUnit的运行环境,我们可以方便地进行单元测试和集成测试。 总结来说,Spring在Eclipse的使用相对比较简单和方便。通过安装Spring插件,我们可以在Eclipse创建、配置和编写Spring项目,并且利用插件提供的功能来简化开发和调试过程。使用Eclipse进行Spring开发,可以大大提高开发效率。 ### 回答3: Spring是一个开源的轻量级Java框架,它提供了很多常用的功能、模块和类库,可以帮助我们快速构建企业级应用程序。 在Eclipse使用Spring有以下几个步骤:首先,需要在Eclipse安装Spring插件。可以通过"Eclipse Marketplace"或者手动下载插件安装包进行安装。安装完成后,重启Eclipse。 接下来,在Eclipse创建一个新的Java项目或者打开现有的Java项目。在项目的构建路径添加Spring相关的依赖库。可以通过"Maven"或者手动下载jar包的方式添加依赖。依赖库包括Spring核心库spring-core.jar等,具体的依赖库根据项目需求而定。 然后,在项目创建一个新的Spring配置文件。Spring配置文件使用XML格式,用于定义Spring容器的Bean以及它们之间的依赖关系。可以命名为"applicationContext.xml"或者其他命名,但需要在项目正确引用该配置文件。 接下来,在Java类使用Spring框架提供的注解或者XML配置方式定义Bean,并在需要使用的地方通过@Autowired等注解实现依赖注入。通过依赖注入,我们可以方便地管理对象之间的依赖关系,避免手动创建对象和依赖传递的麻烦。 最后,在Eclipse配置项目的启动方式,并启动项目。在项目启动时,Spring框架会自动加载配置文件,初始化Bean,并将其放入Spring容器。我们可以通过Spring容器获取Bean,并使用它们的功能。 总结来说,使用Spring框架在Eclipse的步骤包括安装Spring插件、添加Spring相关依赖库、创建Spring配置文件、定义和注入Bean,最后配置项目启动方式并启动项目。通过这些步骤,我们可以方便地在Eclipse使用Spring框架开发Java应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值