工具类

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

转自https://www.cnblogs.com/mrx520/p/7802831.html

SpringBoot普通类中如何获取其他bean例如Service、Dao(转)的更多相关文章

  1. 【blog】SpringBoot普通类中如何获取其他bean例如Service、Dao

    自己写工具类 工具类 import org.springframework.beans.BeansException; import org.springframework.context.Appli ...

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

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

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

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

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

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

  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之使用mybatis generator自动生成bean、mapper、mapper xml

    一.下载mybatis generator插件 二.生成generatorConfig.xml new一个generatorConfig.xml 三.修改generatorConfig.xml 里面的 ...

随机推荐

  1. 初探和实现websocket心跳重连(npm: websocket-heartbeat-js)

    提示:文章最下方有仓库地址 心跳重连缘由 websocket是前后端交互的长连接,前后端也都可能因为一些情况导致连接失效并且相互之间没有反馈提醒.因此为了保证连接的可持续性和稳定性,websocket ...

  2. 断言(assert)

    断言(assert):用来调试.测试代码. 格式: assert 布尔表达式: 字符串 (如果布尔表达式为false时,这个字符串才会显示). 注意: assert默认是关闭的,使用时需要使用&quo ...

  3. 8.6 正睿暑期集训营 Day3

    目录 2018.8.6 正睿暑期集训营 Day3 A 亵渎(DP) B 绕口令(KMP) C 最远点(LCT) 考试代码 A B C 2018.8.6 正睿暑期集训营 Day3 时间:5h(实际) 期 ...

  4. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  5. Android工程运用阿里freeline10秒快速编译分享

    git地址:https://github.com/alibaba/freeline 目前已经更新到0.6.0版本. 原来编译一次需要几分钟甚至几十分钟的android工程,运用freeline,1分钟 ...

  6. BZOJ 5381 or & Codeforces 623E Transforming Sequence DP+NTT

    两道题题意都是一样的 不过$CF$的模数是$10^9+7$ 很简单的分析发现$A_i$项一定要有一个之前没有出现过的二进制位才能满足条件 考虑$DP$来做 设$f_{i,j}$表示$i$个数用了二进制 ...

  7. 以为是tomcat出现using问题,怎么改都改不好终于找到原因

    我也是醉了被自己打败了,以上问题困扰我半天是时间,百度好久都没有解决.应该打开tomcat的bin下的starup.bat结果打开了tomcat-src中的了,怪不得死活出现不了startup

  8. QT.Qt qmake报错(TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension)

    出错信息 打开左边的"项目" 把右侧的"构建目录"修改成你项目所在的文件夹 再次运行试试 成功!

  9. python+requests接口自动化完整项目设计源码

    前言 有很多小伙伴吵着要完整的项目源码,完整的项目属于公司内部的代码,这个是没法分享的,违反职业道德了,就算别人分享了,也只适用于本公司内部的业务. 所以用例的代码还是得自己去一个个写,我只能分享项目 ...

  10. 使用JCOOKIES创建http cookie

    jCookies,一个功能强大的操作http cookie的jquery插件,他能够让你存储任何数据类型如:字符串,数组,对象等.它通过JavaScript存储Cookies,然后通过服务器端代码如: ...