在非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. EasyUi TreeGrid/DataGrid getChecked BUG

    问题描述 步骤一,选中左边树一个节点,然后出现相应的数据,选中进行保存.如,我选中了前4个节点,上图: 步骤二,我再选tree中第二个节点,进行相应的选中和取消选中treegrid中的节点,并保存. ...

  2. java 执行sql文件

    # 背景 用例执行完毕,期望回滚数据,因此希望执行sql来回滚数据 # 步骤 直接show代码,借助的是mybatis的ScriptRunner /** * 执行xx库下的表备份脚本 * * @par ...

  3. H5实现页面内跳转页面

    <!DOCTYPE html><html><body> <iframe src="/example/html/demo_iframe.html&qu ...

  4. 如何使用socket进行java网络编程(一)

    笔者进来遇到一个项目,一家公司的系统需要在完成自身业务逻辑的同时,接入到某银行的核心系统(这里准确说应该是前置机)进行一系列的账务处理,然后再将账务处理结果返回给该公司系统. 网络通信采用TCP协议. ...

  5. Restframework 视图组件与序列号组件的应用.

    models from django.db import models # Create your models here. class Course(models.Model): title=mod ...

  6. S11 day 93 RestFramework 之 序列化

    1. 表建模 from django.db import models # Create your models here. #文章表 class Article(models.Model): tit ...

  7. Python(网络基础)

    day33 参考:http://www.cnblogs.com/linhaifeng/articles/5937962.html IP协议: 规定网络地址的协议叫ip协议,它定义的地址称之为ip地址, ...

  8. Java并发编程实践读书笔记(4)任务取消和关闭

    任务的取消 中断传递原理 Java中没有抢占式中断,就是武力让线程直接中断. Java中的中断可以理解为就是一种简单的消息机制.某个线程可以向其他线程发送消息,告诉你“你应该中断了”.收到这条消息的线 ...

  9. 使用git工具删除github上的文件或者文件夹

    解决 使用git工具删除github上的文件或者文件夹 当我们需要从github上删除一些我们不需要的文件或者文件夹时,如果通过github来操作的话,将会很麻烦,因为github只允许删除一个仓库, ...

  10. Jasmine入门(下)

    上一篇 Jasmine入门(上) 介绍了Jasmine以及一些基本的用法,本篇我们继续研究Jasmine的其他一些特性及其用法(注:本篇中的例子均来自于官方文档). Spy Spy用来追踪函数的调用历 ...