在非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. HDU1459 非常可乐(BFS) 2016-07-24 15:00 165人阅读 评论(0) 收藏

    非常可乐 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶 ...

  2. B样条参数曲线学习(1)

    B样条参数曲线学习 Bezier曲线有许多优越性,但有两点不足: (1) 特征多边形的顶点个数决定了Bezier曲线的阶次,并且在阶次较大时,特征多边形对曲线的控制将会减弱: (2) Bezier曲线 ...

  3. GoF设计模式学习-单例模式

    1.目的 控制实例的个数,类设计者应该保证只有一个实例,不能将此责任[只有一个实例]强制交给类使用者. 2.整体实现 1.单线程单例模式的实现. using System; using System. ...

  4. WinRT 中检查 WiFi 是否可用

    public static bool IsWifiConnected() { bool isWifiConnected = false; ConnectionProfile currentConnec ...

  5. 为MAC配置终端环境iTerm2+Zsh+oh-my-zsh

    首先展示下我的终端吧. 这就是我们为什么要配置iTerm2+Zsh+oh-my-zsh环境的原因: 我们使用zsh解释器,当然等你使用 zsh时就会知道zsh与bash对比的强大之处了. 至于我们的g ...

  6. Microsoft SQL Server 2012 管理 (2): Auditing

    -- Demostratin 2A (Using SQL Server Audit) -- Step 1: Use the master database USE master; GO -- Step ...

  7. Linux C++ - IP地址转换函数

    1. 函数用途:数字网络序本地序转换 适用类型:IP地址uint32_t类型.端口号uint16_t类型 #include<netinet/in.h> extern uint32_t nt ...

  8. asp.net接收传入的数据流

    我们在日常的应用中,都会遇到这样一个问题,就是我们做的asp.NET程序,会收到其它第三方软件传过来的一些信息数据流,当然了一些文本形式的信息,可以采用get或post的方法来接收,可是要是传过来的是 ...

  9. WebAPI+Html跨域时对session的支持

    1.Global.asax中添加对Session的支持,重新Init方法: public override void Init() { this.PostAuthenticateRequest += ...

  10. OPC测试常用的OPCClient和OPCServer软件推荐

    各位在进行OPC通讯时,常会遇到两种情况: 1)使用一个OPCClient在同一台计算机上连接远程计算机上的多个OPCServer时,发现某个OPCServer是通畅的,但其他的OPCServer却无 ...