Java SpringBoot 中,動態執行 bean 物件中的方法

VipSoft發表於2023-04-14

根據不同的條件,呼叫不同的 bean 物件,執行物件中的方法

SpringUtils 工具類

package com.vipsoft.web.utils;


import cn.hutool.core.util.ArrayUtil;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring工具類 方便在非 Spring 管理環境中獲取bean
 *
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {

    /**
     * Spring 應用上下文環境
     */
    private static ConfigurableListableBeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

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

    /**
     * 獲取物件
     *
     * @param name
     * @return Object 一個以所給名字註冊的bean的例項
     * @throws BeansException
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 獲取型別為requiredType的物件
     *
     * @param clz
     * @return
     * @throws BeansException
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一個與所給名稱匹配的bean定義,則返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return beanFactory.containsBean(name);
    }

    /**
     * 判斷以給定名字註冊的bean定義是一個singleton還是一個prototype。 如果與給定名字相應的bean定義沒有被找到,將會丟擲一個異常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws NoSuchBeanDefinitionException
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 註冊物件的型別
     * @throws NoSuchBeanDefinitionException
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getType(name);
    }

    /**
     * 如果給定的bean名字在bean定義中有別名,則返回這些別名
     *
     * @param name
     * @return
     * @throws NoSuchBeanDefinitionException
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getAliases(name);
    }

    /**
     * 獲取aop代理物件
     *
     * @param invoker
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker) {
        return (T) AopContext.currentProxy();
    }

    /**
     * 獲取當前的環境配置,無配置返回null
     *
     * @return 當前的環境配置
     */
    public static String[] getActiveProfiles() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 獲取當前的環境配置,當有多個環境配置時,只獲取第一個
     *
     * @return 當前的環境配置
     */
    public static String getActiveProfile() {
        final String[] activeProfiles = getActiveProfiles();
        return ArrayUtil.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
    }
}

執行類

package com.vipsoft.web.task;

import cn.hutool.core.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component("scheduletask")
public class ScheduleTask {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void execute(String param) {
        logger.info("執行 Schedule Task,當前時間:{},任務引數:{}", DateUtil.now(), param);
    }
}

測試類

package com.vipsoft.web;

import cn.hutool.core.util.StrUtil;
import com.vipsoft.web.utils.SpringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.lang.reflect.Method;

@SpringBootTest
public class SpringUtilTest {

    @Test
    void invokeTest() throws Exception {
        //點前面的做為 bean 名稱,後面是方法名,(中是引數==引數可以放JSON)
        String invokeTarget = "scheduletask.execute('VipSoft Quartz')";
        invokeMethod(invokeTarget);
    }

    void invokeMethod(String invokeTarget) throws Exception {
        String beanName = StrUtil.subBefore(invokeTarget, ".", true);
        String methodName = StrUtil.subBetween(invokeTarget, ".", "(");
        String param = StrUtil.subBetween(invokeTarget, "(", ")");

        Object bean;
        if (StrUtil.count(beanName, ".") == 0) {
            bean = SpringUtils.getBean(beanName);
        } else {
            //Package 的形式得到 Bean,如: beanName="com.vipsoft.web.task.ScheduleTask"
            bean = Class.forName(beanName).newInstance();
        }

        if (bean != null) {
            if (StrUtil.isNotEmpty(param)) {
                Method method = bean.getClass().getDeclaredMethod(methodName, String.class);
                method.invoke(bean, param);
            } else {
                Method method = bean.getClass().getDeclaredMethod(methodName);
                method.invoke(bean);
            }
        }
    }

}

相關文章