动态获取spring管理的bean工具类

简介: 动态获取spring管理的bean工具类

1、说明

java中利用反射去动态执行一个普通类的方法一般是非常简单的,但是遇到spring管理的bean类可能就不太好做了,这里给出以下方法解决这个问题。主要思路是用spring上下文获取bean的实例对象,然后用目标对象的代理对象反射执行方法。

2、工具类代码

package com.ourlang.dataextract.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
 * 动态获取spring管理的bean实例对象
 * spring上下文工具类
 * https://github.com/ourlang
 * @author ourlang
 */
@Component("springContextUtil")
public class SpringContextUtil implements ApplicationContextAware {
    /**
     * Spring应用上下文环境
     */
    private static ApplicationContext applicationContext;
    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
    /**
     * 获得spring上下文
     *
     * @return ApplicationContext spring上下文
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    /**
     * 获取bean
     *
     * @param name service注解方式name为小驼峰格式
     * @return Object bean的实例对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) applicationContext.getBean(name);
    }
    /**
     * 获取bean
     *
     * @param clz service对应的类
     * @return Object bean的实例对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<?> clz) throws BeansException {
        return (T) applicationContext.getBean(clz);
    }
}

3、调用测试

    @Test
    public void testSpringContext(){
        // 获取bean,注意这里用实现类的接口强转去获得目标bean的代理对象,才能成功执行下面的反射方法
        //com.ourlang.dataextract.service.impl.SInpatientInRoundsServiceImpl@246dafe6
        Object obj= SpringContextUtil.getBean(ISInpatientInRoundsService.class);
        // 获取方法,这个hello实际上是父类的方法,后面String.class是参数类型
        Method method= ReflectionUtils.findMethod(obj.getClass(),"hello",String.class);
        // 反射执行方法,ourlang是后面传递的参数
        ReflectionUtils.invokeMethod(method,obj,"ourlang");
    }
相关文章
|
8天前
|
安全 Java Spring
Spring框架中的单例Bean是线程安全的吗?
Spring框架中的单例Bean是线程安全的吗?
11 1
|
8天前
|
XML 前端开发 Java
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
11 0
|
8天前
|
存储 缓存 Java
【JavaEE】Spring中注解的方式去获取Bean对象
【JavaEE】Spring中注解的方式去获取Bean对象
3 0
|
8天前
|
存储 Java 对象存储
【JavaEE】Spring中注解的方式去存储Bean对象
【JavaEE】Spring中注解的方式去存储Bean对象
10 0
|
8天前
|
存储 Java 对象存储
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
9 0
|
8天前
|
开发框架 监控 Java
深入探索Spring Boot的监控、管理和测试功能及实战应用
【5月更文挑战第14天】Spring Boot是一个快速开发框架,提供了一系列的功能模块,包括监控、管理和测试等。本文将深入探讨Spring Boot中监控、管理和测试功能的原理与应用,并提供实际应用场景的示例。
19 2
|
8天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
22 1
|
8天前
|
XML Java 数据格式
Spring Bean
【4月更文挑战第30天】Spring Bean
17 0
|
8天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
41 2
|
8天前
|
XML Java 数据格式
谈谈 Spring 中 Bean 的生命周期?
谈谈 Spring 中 Bean 的生命周期?
20 1