springboot-9-在springboot中引入bean
在非spring管理的包中引入spring管理的类, 可以使用一个类继承ApplicationContextAware即可
分两种, 第一种该类在spring的包扫描范围之下:
package com.iwhere.test.util; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* 在普通类中引入spring中的bean, 在springboot的扫描包下
* @ServletComponentScan
*
* 如果不在springboot的包扫描下, 则不需要component, 但需要在App.java中引入
* @Import(value={SpringAppUtils.class})
*
* @author 231
* @time 2017年3月15日 下午2:40:32 2017
*/
@Component
public class SpringAppUtils implements ApplicationContextAware {
private static Logger LOGGER = LoggerFactory.getLogger(SpringAppUtils.class);
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
if (SpringAppUtils.applicationContext == null) {
applicationContext = context;
}
LOGGER.info("springAppUtils is init");
} /**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 通过那么获取bean
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
} /**
* 通过class获取bean
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
} /**
* 通过name和class获取bean
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
第二种: 配置类不在springboot扫描之下:
此时类上不需要加 @Component 注解
但需要在App.java中引入
@Import(value={SpringAppUtils.class})
@SpringBootApplication
@ServletComponentScan
@Import(value={SpringUtil.class})
publicclass App { {main} }
之后有一次在监听器中使用到, 感觉这两种方法都不太好用, 于是有了第三种:
mongoTemplate = WebApplicationContextUtils.getWebApplicationContext(servlet).getBean(MongoTemplate.class);
这个可以直接获取到springboot的工厂, 然后使用工厂的方法获取bean对象, 需要传入一个servlet对象, 可在controller或者listener中使用
4, 获取spring的上下文:
package com.iwhere.demo.factory; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* 获取spring上下文
* @author wenbronk
* @time 2017年4月18日 下午12:49:59 2017
*/
@Component
public class SpringContext implements ApplicationContextAware {
private static Logger LOGGER = LoggerFactory.getLogger(SpringContext.class);
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (applicationContext == null) {
applicationContext = arg0;
}
LOGGER.info("spring application context is init");
} /**
* 获取applicationContext
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 通过name获取bean
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
} /**
* 通过class获取bean
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
} /**
* 通过name和class获取bean
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
} }
原文地址: http://412887952-qq-com.iteye.com/blog/2292388
springboot-9-在springboot中引入bean的更多相关文章
- SpringBoot中的bean加载顺序
https://www.dazhuanlan.com/2019/10/22/5daebc5d16429/ 最近在做传统Spring项目到SpringBoot项目迁移过程中,遇到了一些bean加载顺序的 ...
- SSM项目 以及 springboot 中引入swagger2的方法
swagger2是一个非常好用的接口文档,在开发的过程中方便前后端接口的交接. 下面我们就来讲讲在使用java时,分别在SSM框架,以及springboot+mybatis框架中引入swagger2的 ...
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
- 为什么在SpringBoot+maven的项目中,所引入的依赖包可以不指定依赖的版本号?
当在Springboot项目中引入了spring-boot-starter-parent,则可以不用引入依赖包版本号,比如: <parent> <groupId>org.spr ...
- SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean
SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean 在SpringBoot中开发AspectJ时,使用CTW的方式来织入代码,由于采用这种形式,切面Bean ...
- SpringBoot学习(七)-->SpringBoot在web开发中的配置
SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...
- springboot+redis+虚拟机 springboot连接linux虚拟机中的redis服务
文章目录 1.前提条件:确保虚拟机开启.并且连接到redis 2.新建立一个springboot项目,创建项目时勾选web选项 3.在pom中引入redis依赖 4.在application.prop ...
- 尚硅谷springboot学习10-@PropertySource,@ImportResource,@Bean
@PropertySource 使用指定的属性文件而不一定是application.xxx 同样可以注入相关内容 @ImportResource 导入Spring的配置文件,让配置文件里面的内容生效: ...
- SpringBoot配置——@PropertySource、@ImportResource、@Bean
@PropertySource:加载指定的配置文件 package com.hoje.springboot.bean; import org.springframework.beans.factory ...
随机推荐
- hdu 4974 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...
- function [eigf,eigv,dof]=laplaceeig(node,elem,problem)
function [eigf,eigv,dof]=laplaceeig(node,elem,problem) % -boundary eigenvalue problem % problem='0-b ...
- Elasticsearch 核心插件Kibana 本地文件包含漏洞分析(CVE-2018-17246)
不久前Elasticsearch发布了最新安全公告, Elasticsearch Kibana 6.4.3之前版本和5.6.13之前版本中的Console插件存在严重的本地文件包含漏洞可导致拒绝服务攻 ...
- Asp.Net Mvc ScriptBundle 脚本文件捆绑压缩 导致 脚本出错的问题
由于捆绑压缩会对所有包含的文件进行压缩,无法设置忽略对某个js文件的压缩.导致压缩该js后,脚本出错的问题. 解决方式: 重写 ScriptBundle 的 GenerateBundleRespons ...
- linux 基本操作笔记
linux文件系统的实现 linux有一个树状结构来组织文件,数的顶端为根目录/,节点为目录,而末节点为所包含的数据文件.我们可以对文件进行多种操作,比如打开和读写. 存储设备分区 文 ...
- JavaScript基础事件(6)
day53 参考:https://www.cnblogs.com/liwenzhou/p/8011504.html#autoid-2-3-8 事件 HTML 4.0 的新特性之一是有能力使 HTML ...
- [ActionScript 3.0] UDP通信
package com.controls.socket { import flash.events.DatagramSocketDataEvent; import flash.events.Event ...
- 01-Python的基础知识1
基础即常识. - Python的对象模型 - Python中一切皆对象. - 内置对象:数字,字符串,列表,元组,集合等等. - 基本输入 - 基本模式 变量 = input("提示字 ...
- MySQL大量线程处于Opening tables的问题分析
[作者] 王栋:携程技术保障中心数据库专家,对数据库疑难问题的排查和数据库自动化智能化运维工具的开发有强烈的兴趣. [问题描述] 最近有一台MySQL5.6.21的服务器,在应用发布后,并发线程Thr ...
- HTML+Javascript制作拼图小游戏详解(二)
上一篇我们说了网页的基本布局.接下来将为大家带来具体的实现方法. 拼图通表格来实现,做一个方形的表格,改变其大小使之如图所示. 试想一下如果我们将一张图片剪成6张分别放入对应位置,然后再把它打乱,这样 ...