首页 > 其他分享 >SpringBoot中通过ApplicationContext获取bean对象

SpringBoot中通过ApplicationContext获取bean对象

时间:2022-12-19 15:12:14浏览次数:40  
标签:ApplicationContext SpringBoot springframework bean context org import redisTempl

1.定义一个工具类

package com.example.redis.utils;

import com.example.redis.DemoApplication;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtils implements ApplicationContextAware{
    //放置在获取bean的时候提示空指针,将其定义为静态变量
    private static ApplicationContext context;

   //类初始化完成之后调用setApplicationContext()方法进行操作
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtils.context = applicationContext;
    }

   public static ApplicationContext geteContext(){
        return context;
    }

   public static Object getBean(String beanName){
        //在这一步的时候一定要注意,此时可调用这个方法的时候
        //context可能为空,会提示空指针异常,需要将其定义成静态的,这样类加载的时候
        //context就已经存在了
        return context.getBean(beanName);
    }
}

 

2.调用getBean()方法(注意首字母要小写)

RedisTemplate<Object,Object> redisTemplate =
                    (RedisTemplate<Object,Object>) ApplicationContextUtils.getBean("redisTemplate");
        //对key进行序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForHash().put(id.toString(),key.toString(),value);

 

标签:ApplicationContext,SpringBoot,springframework,bean,context,org,import,redisTempl
From: https://www.cnblogs.com/mr-wuxiansheng/p/16992200.html

相关文章

  • docker-compose入门以及部署SpringBoot+Vue+Redis+Mysql(前后端分离项目)以若依前后端
    场景若依前后端分离版手把手教你本地搭建环境并运行项目:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662上面在搭建起来前后端分离版的项目后。......
  • Springboot整合Apache Dubbo
    ​​Springboot​​​整合​​ApacheDubbo​​一、通过​​Docker​​​安装​​Zookeeper​​​​docker-compose​​​的​​yaml​​文件version:'3'services:zoo......
  • springboot+postgresql集成anyline试水
    anyline是什么简单讲就是一个工具可以让你抛开常规的机械性建mapper、dao、sql,用通用的语句查询和操作数据库表。目前也在初步探索中,感受还不深。官网文档:http://doc.any......
  • 5. MinIO与springboot的集成
    MinIO与springboot的集成搭建一个springboot的项目,集成MinIO实现文件的管理。一、搭建springboot环境IntelliJIDEAJDK17gradle-7.5.1springboot2.7.6项目地址:g......
  • springboot实现AOP切面编程
    概述AOP(AspectOrientedProgramming)即面向切面编程。面向切面是面向对象中的一种方式而已。在代码执行过程中,动态嵌入其他代码,叫做面向切面编程(将交叉业务逻辑封装成成......
  • 亲手实现一个springboot默认配置&起步加载
    实现一、默认配置1、创建springboot项目引入spring-boot-dependencies依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-......
  • 【java】(二) SpringBoot 源码解析——run启动加载过程——准备环境
    1.前言深入学习springboot笔记系列,可能会有错误还请指正,互相勉励,互相学习。上一章讲了SpringApplicaiton是如何初始化的,本章讲解后续的run方法的启动过程。本章涉及......
  • SpringBoot3.x中spring.factories功能被移除的解决方案
    背景笔者所在项目组在搭建一个全新项目的时候选用了​​SpringBoot3.x​​​,项目中应用了很多​​SpringBoot2.x​​​时代相关的第三方组件例如​​baomidou​​​出品的​......
  • springboot项目打包报错:Type org.springframework.boot.maven.RepackageMojo not pres
    折磨了好久的一个问题,换过spring-boot-maven-plugin版本,但不见效。今天参考这篇文章:springboot打包RepackageMojonotpresent,版本改成2.6.2,不行。就想,可能是我用的idea......
  • SpringBoot知识点
    自动装配原理SpringBoot特点优点:(1)创建独立Spring应用(2)内嵌web服务器(3)自动start依赖,简化构建配置(4)自动配置Spring以及第三方功能(5)提供生产级别的监控、健康检测以及......