在非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的更多相关文章

  1. SpringBoot中的bean加载顺序

    https://www.dazhuanlan.com/2019/10/22/5daebc5d16429/ 最近在做传统Spring项目到SpringBoot项目迁移过程中,遇到了一些bean加载顺序的 ...

  2. SSM项目 以及 springboot 中引入swagger2的方法

    swagger2是一个非常好用的接口文档,在开发的过程中方便前后端接口的交接. 下面我们就来讲讲在使用java时,分别在SSM框架,以及springboot+mybatis框架中引入swagger2的 ...

  3. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  4. 为什么在SpringBoot+maven的项目中,所引入的依赖包可以不指定依赖的版本号?

    当在Springboot项目中引入了spring-boot-starter-parent,则可以不用引入依赖包版本号,比如: <parent> <groupId>org.spr ...

  5. SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean

    SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean 在SpringBoot中开发AspectJ时,使用CTW的方式来织入代码,由于采用这种形式,切面Bean ...

  6. SpringBoot学习(七)-->SpringBoot在web开发中的配置

    SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...

  7. springboot+redis+虚拟机 springboot连接linux虚拟机中的redis服务

    文章目录 1.前提条件:确保虚拟机开启.并且连接到redis 2.新建立一个springboot项目,创建项目时勾选web选项 3.在pom中引入redis依赖 4.在application.prop ...

  8. 尚硅谷springboot学习10-@PropertySource,@ImportResource,@Bean

    @PropertySource 使用指定的属性文件而不一定是application.xxx 同样可以注入相关内容 @ImportResource 导入Spring的配置文件,让配置文件里面的内容生效: ...

  9. SpringBoot配置——@PropertySource、@ImportResource、@Bean

    @PropertySource:加载指定的配置文件 package com.hoje.springboot.bean; import org.springframework.beans.factory ...

随机推荐

  1. hdu 4957 贪心破木桶接水大trick

    http://acm.hdu.edu.cn/showproblem.php?pid=4957 拿n只破的木桶去接水,每只木桶漏水速度为a[i],最后要得到b[i]单位的水,自来水的出水速度为V,木桶里 ...

  2. hdu 5025 bfs+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙 ...

  3. How To Change the Supplier Bank Account Masking in UI (Doc ID 877074.1)

      Give Feedback...           How To Change the Supplier Bank Account Masking in UI (Doc ID 877074.1) ...

  4. 【Win10】文件拖放打开

    在 Windows 10 中,通用应用程序在桌面环境下是支持从资源管理器拖放文件打开的. 这篇博文将演示拖放图片或文本文件,并在程序中打开显示. 前台 XAML: <Page x:Class=& ...

  5. 动态类型和匿名类型在asp.net webapi中的应用

    1.  动态类型用于webapi调用 假设需要调用一个webapi,webapi返回了一个json字符串.字符串如下: {"ProductId":"AN002501&qu ...

  6. Linq to SQL 参考Demo

    LINQ to SQL语句()之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句 ...

  7. 程序媛计划——python初级class5~13

    列表和元组都是可迭代对象(可以用于for in) 列表 [] #添加列表元素: list.append(argu) #修改列表: list[2] = 2017 #删除列表元素 Del list[2] ...

  8. UIVisualEffectView(高斯模糊效果)

    ///高斯模糊. UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; tempView. ...

  9. 在Load average 高的情况下如何鉴别系统瓶颈

    在Load average 高的情况下如何鉴别系统瓶颈.是CPU不足,还是io不够快造成? 或是内存不足? 一:查看系统负载vmstat procs -----------memory-------- ...

  10. IntelliJ IDEA 2016 破解旗舰版

    JRebel 授权服务器http://idea.lanyus.com/ilanyulanyu19950316@gmail.com