自己写工具类

工具类

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

/**
 * Author:Mr.X
 * Date:2017/11/8 10:00
 * Description:
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

使用方法

public class ArticleFormConverter {

    private ArticleRepository articleRepository = (ArticleRepository) SpringContextUtils.getBean(ArticleRepository.class);

    public Article convert(ArticleForm articleForm) {
        // 更新
        if (articleForm.getId() != null) {
            Article article = articleRepository.findOne(articleForm.getId());
            BeanUtils.copyProperties(articleForm, article);
            article.setHtmlContent(Processor.process(article.getContent()));
            return article;
        }

        // 添加
        Article article = new Article();
        BeanUtils.copyProperties(articleForm, article);
        article.setHtmlContent(Processor.process(article.getContent()));
        // 添加时其他需要默认设置的属性值
        article.setReadSize(0);
        article.setStatus(ArticleStatus.UP_SHELVES.getCode());  // 默认为上架
        article.setCreateTime(new Date());
        article.setUserId(1); // TODO 暂定为,应该从session中取
        return article;
    }
}

参考链接

第三十二章:如何获取SpringBoot项目的applicationContext对象:http://www.jianshu.com/p/3cd2d4e73eb7

手动获取spring的ApplicationContext和bean对象:http://www.cnblogs.com/yangzhilong/p/3949332.html

【blog】SpringBoot普通类中如何获取其他bean例如Service、Dao的更多相关文章

  1. SpringBoot普通类中如何获取其他bean例如Service、Dao(转)

    工具类 import org.springframework.beans.BeansException; import org.springframework.context.ApplicationC ...

  2. .netcore2.1在控制器中和类中,获取appsettings中值的方法

    一般我们在开发项目中,都会从配置文件中获取数据库连接信息.自定义参数配置信息等. 在.netcore中在控制器和自定义类中,获取配置文件中参数方式如下: appsettings.json { &quo ...

  3. 教你在Java的普通类中轻松获取Session以及request中保存的值

    曾经有多少人因为不知如何在业务类中获取自己在Action或页面上保存在Session中值,当然也包括我,但是本人已经学到一种办法可以解决这个问题,来分享下,希望对你有多多少少的帮助! 如何在Java的 ...

  4. Springboot中如何在Utils类中使用@Autowired注入bean

    Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类Statisti ...

  5. springboot @Value 类中读取配置文件 .properties null 原因和解决方案

    问题:在一个工具类中,通过@Value来映射配置文件的值,得到的总是null 原因:不能用new工具类的方式,应该是用容器注册(@Autowried)的方式使用此工具类,就能得到配置文件里的值 上代码 ...

  6. spring 在静态工具类中使用注解注入bean

    /** * @author: jerry * @Email: * @Company: * @Action: 日志处理工具类 * @DATE: 2016-9-19 */ @Component//泛指组件 ...

  7. SpringBoot的配置属性文件*.properties值如何映射到类中使用

    想要在JAVA Bean中读取配置文件中的内容有两种方式,可以进行获取到 第一种方式: 1.在默认的配置文件application.properties 中进行设置 Key-Value键值对 com. ...

  8. 静态工具类中使用注解注入service

    转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是 ...

  9. springboot在eclipse中运行使用开发配置,打包后运行使用生产环境默认配置

    java命令运行springboot jar文件,指定配置文件可使用如下两个参数中其中一个 --spring.config.location=配置文件路径 -Dspring.profiles.acti ...

随机推荐

  1. Day037--Python--线程的其他方法,GIL, 线程事件,队列,线程池,协程

    1. 线程的一些其他方法 threading.current_thread()  # 线程对象 threading.current_thread().getName()  # 线程名称 threadi ...

  2. session会话对象

    一.session会话对象介绍: 会话对象让你能够跨请求保持某些参数,它也会在同一个session实例发出的所有请求之间保持cookie. 二.步骤 1.对session对象进行一次实例化 2.进行登 ...

  3. Codeforces Round #518 (Div. 2) B LCM

    传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题解: 这道题有点意思,有点数学的味道. 根据定义“[a,b] / a”可得这求得是l ...

  4. springmvc 学习笔记

    @Autowired,@RequestMapping,@RequestParam 使用该注解,引入对象时, 可以省略setter getter.减少代码显示. @AutowiredSimService ...

  5. 为什么fork()2次会避免产生僵尸进程

    什么是僵尸进程:用fork()创建子进程后,子进程已终止但父进程没有对它进行善后处理,那么子进程的进程描述符就一直保存在内存中,子进程就是僵尸进程. 怎么产生僵尸进程: 1.父进程没有SIGCHLD信 ...

  6. Vue(基础六)_嵌套路由(续)

    一.前言                  1.路由嵌套里面的公共路由                  2.keep-alive路由缓存                  3.导航守卫 二.主要内容 ...

  7. nginx安装配置: configure命令

    configure命令用来配置nginx编译环境. 该命令定义了系统各方面功能,包括允许nginx使用的连接处理方式. 其执行结果是生成一个Makefile文件. configure命令支持如下参数: ...

  8. 序列化serialize与反序列化unserialize

    有利于存储和传递value(除了resource类型外),却不会丢失其原有类型和结构. serialize序列化时会调用魔术方法__sleep(); unserialize反序列化时会调用魔术方法__ ...

  9. angular中因异步问题产生的错误解决方法

    方法一 private userTaskList(){ let auth = this.make_basic_auth("kermit","kermit"); ...

  10. pt-online-schema-change 测试使用-包含生成测试数据

    pt-online-schema-change 测试使用-包含生成测试数据 # 参考网址: https://www.2cto.com/database/201703/618280.html 一.简要描 ...