优雅获取ApplicationContext上下文对象,ApplicationContextUtil上下文获取工具类

本文主要介绍在Java编程中,借助Spring框架获取容器中Bean对象的方法。具体包括实现ApplicationContextAware接口来获取上下文对象,以及利用SpringBoot启动类的setApplicationContext方法获取上下文对象,还提及了应用上下文帮助类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


提示:以下是本篇文章正文内容,下面案例可供参考

一、实现接口实现ApplicationContextAware接口获取上下文对象

package com.example.demo.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

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

    public static <T> T getBean(String beanName) {
        return (T)applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> className) {
        return applicationContext.getBean(className);
    }
}

二、SpringBoot启动类setApplicationContext获取上下文对象

①SpringBoot启动类

package com.example.demo;

import com.example.demo.config.ApplicationContextUtil2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

	public static void main(String[] args) {
		ApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
		//设置上下文工具类中对象
		ApplicationContextUtil2.setApplicationContext(applicationContext);
	}

}

②ApplicationContext应用上下文帮助类

package com.example.demo.config;

import org.springframework.context.ApplicationContext;

public class ApplicationContextUtil2 {

    private static ApplicationContext applicationContext;

    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

    public static <T> T getBean(String beanName) {
        return (T)applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> className) {
        return applicationContext.getBean(className);
    }
}

### Spring 工具类获取 ApplicationContext 对象Spring 框架中,`ApplicationContext` 是核心接口之一,提供了访问应用上下文中各种 Bean 的功能。为了方便开发者更灵活地获取 `ApplicationContext` 实例,通常可以借助工具类来实现这一目标。 以下是几种常见的通过工具类获取 `ApplicationContext` 的方式: #### 方法一:继承 `ApplicationObjectSupport` 通过让工具类继承 `org.springframework.context.ApplicationObjectSupport` 抽象类,并将其标记为组件 (`@Component`) 以便被 Spring 容器管理,在容器启动时会自动调用父类的 `setApplicationContext()` 方法完成注入[^1]。之后可通过重载的方法 `getApplicationContext()` 获得当前的应用上下文实例。 ```java import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationObjectSupport; import org.springframework.stereotype.Component; @Component public class ApplicationContextUtil extends ApplicationObjectSupport { /** * 获取SpringApplicationContext对象 */ public static ApplicationContext getApplicationContext() { return applicationContextHolder.get(); } private static final ThreadLocal<ApplicationContext> applicationContextHolder = new ThreadLocal<>(); @Override protected void initApplicationContext() throws BeansException { super.initApplicationContext(); applicationContextHolder.set(getApplicationContext()); } } ``` #### 方法二:自定义静态变量存储 另一种常见的方式是在工具类内部声明一个静态成员变量保存 `ApplicationContext` 参考引用[^2]。当 Spring 初始化完成后,利用事件监听机制或者手动设置该字段值即可供后续使用。 ```java import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class CustomApplicationContext implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static Object getBean(String beanName){ return context.getBean(beanName); } } ``` #### 方法三:模拟实现 ApplicationContext 接口 对于某些特殊场景下可能需要自行扩展或简化版的功能支持,则可以根据实际需求重新设计并实现自己的 `ApplicationContext` 类型结构[^3]。不过这种方式较少见于生产环境之中,更多适用于学习研究目的。 关于可能出现 NullPointerException 错误的情况分析如下: 如果按照上述任一种方案操作仍遇到空指针异常问题,除了确认 Servlet 部分逻辑无误外,还需仔细核查是否存在 XML 文件路径拼接错误、命名空间未正确定义等问题导致依赖注入失败情形发生[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值