SpringContextUtil工具类

使用@Component时再使用@Resource或@Autowired时注入失败问题 在使用@Component注解将bean实例化到spring容器内的时候,@Autowired是在这个bean之中的,@Autowired还未完成自动装载,所以导致service为null。最近在写java项目时发现在使用了@Component的类同时使用@Autowired自动注入service或者mapper的时候发现并未注入成功,得到的对象是null.@Autowired注解放在方法上会在类加载后自动注入这个方法的参数,并执行一遍方法。 阅读详情

使用SpringContextUtil工具类来获取Spring容器管理的bean。

创建一个SpringContextUtil类 实现ApplicationContextAware,并实现其方法(注:SpringContextUtil加注解@Component)。

@Component
public class SpringContextUtil implements  ApplicationContextAware {
    
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 手动在Spring容器中获取一个对象
     */
    public static <T> T getBean(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }

    /**
     * 通过名称获取bean
     */
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }
}

在普通类中调用SpringContextUtil中的getBean方法即可获取 Spring容器管理的bean对象

public class test(){
    Student student= SpringContextUtil.getBean(Student.class);
}

SpringUtils.getBean 空指针异常问题 jeecgboot集成flowable中SpringUtils.getBean 空指针异常问题 阅读详情

相关推荐

spring static静态方法里面注入bean空指针解决

最近在跑一个公司花钱买的古老区块链,项目15年开发的。遇到了一个注入问题。该项目中注入jedisPool时使用了getBean()注入,然而怎么跑都是NullPoint,一翻扣头发后得已解决。 解决方法1 使用@Autowired的方式注入 必须在类上加@Component。我是用的这种 @Component public class KlineDataUtil { private static RedisService redisService; @Autowired public void set(RedisService redisService { KlineDataUtil

神奇的SpringContextUtil

不大不小算是个知识点吧

qq_34787180的博客 4577

HFSS实战:手把手教你设计微带发夹线滤波器(附完整参数计算流程)

本文详细介绍了使用HFSS设计微带发夹线滤波器的完整流程,包括参数计算、HFSS建模和优化策略。通过实战案例,帮助读者掌握微波滤波器设计的核心技巧,特别是奇偶模阻抗计算和HFSS仿真中的关键设置,适用于通信系统设计和射频工程应用。

weixin_29251567的博客 402

Spring获取注入Bean

package com.clicks.config; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * @author

weixin_48314739的博客 972

封装SpringContext工具类SpringContextUtil

实现自己的SpringContext工具类 记录一下自己封装的SpringContextUtil工具类 package com.xiaoju.automarket.fate.data.engine.core.service.impl.utils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import o

User_kh的博客 812

SpringContextUtil工具类获取spring容器中的bean

package cc.mrbird.febs.common.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /**.

qq_42298793的博客 1124

主动获取spring容器工具类SpringContextUtil

/** * 获取spring容器,以访问容器中定义的其他bean */ @Component public class SpringContextUtil implements ApplicationContextAware { // Spring应用上下文环境 @Autowired private ApplicationContext applicationCont

飞翔中的菜鸟 8868

Spring Boot中通过SpringContextUtil工具类获取容器中Bean对象

在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所管理的,如果直接在所依赖的属性上使用@Autowired就会报无法注入的错误,或者是没报错,但是使用的时候会报空指针异常。总而言之由于其是不受Spring IoC容器所管理的,因而无法注入。 Spring的核心是ApplicationContext,它负责管理 beans 的完整生命周期。我们可以从applicatio

分享开发、架构设计、容器、云原生、大数据、人工智能等等 9043

SpringContextUtil 获取IOC容器工具类

获取容器中的Bean。SpringContextUtil工具类

输入技术、输出想法 611

SpringContextUtil工具类-获取spring中的bean对象

/** * 获取spring中的bean对象工具类 * @date 2018-07-23 17:42 */ @Component public class SpringContextUtil implements ApplicationContextAware { /** * Spring应用上下文环境 */ private static Appl...

zzjstudent的专栏 1万+

SpringContextUtil工具类实现

1.先写个工具类,实现ApplicationContextAware接口,然后他会把ApplicationContext对象传给你,在setApplicationContext方法里已经把ApplicationContext传给你了,然后去完成初始化。 public class SpringContextUtil implements ApplicationContextAware{ priv

name_liv的专栏 4215

SpringContextUtil spring上下文获取工具类

package com.midea.biz; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; impor...

weixin_30924087的博客 432

static方法中调用接口,SpringContextUtil工具类实现

1.先写个工具类,实现ApplicationContextAware接口,然后他会把ApplicationContext对象传给你,在setApplicationContext方法里已经把ApplicationContext传给你了,然后去完成初始化。 public class SpringContextUtil implements ApplicationContextAware{ privat

长江量子 2053

java技术--SpringContextUtil类的作用

企业级开发过程中,一般使用Spring注解开发。使用注解获取Spring容器管理的类(这些类都必须是Spring容器中的类)! 开发过程中,有时会遇到需要在普通类中注入spring管理的单例bean。直接注入是注入不进去的,这是就需要创建一个工具类来获取Spring容器管理的bean。 举例:普通类User.class public class User{ ...

qq591009234的博客 8254

SpringContextUtil 的配置和调用

首先:在springmvc里面配置 <bean id="springContextUtil" class="com.hna.hka.rmc.common.util.SpringContextUtil" lazy-init="false"></bean>然后编写工具类: package com.ece.manager.web.controller;import org.s...

weixin_30596023的博客 429

Spring获取bean实例最佳方式-SpringContextUtil

Spring获取bean实例最佳方式-SpringContextUtil

inexaustible的博客 884

获取SPRING BEAN工具类SPRINGCONTEXTUTIL

https://www.cnblogs.com/lihaoyang/p/12582239.html

pb951028的博客 216
下一篇: Java中判断日期是否超过一年
宋宋和她的喵
博客等级 码龄5年 2粉丝 3原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值