001/* 002 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.dromara.warm.flow.core.invoker; 017 018import java.util.function.Function; 019 020/** 021 * 获取bean方法 022 * 023 * @author warm 024 */ 025public class FrameInvoker<M> { 026 027 public static FrameInvoker frameInvoker = new FrameInvoker<>(); 028 029 private Function<Class<M>, M> beanFunction; 030 031 private Function<String, String> cfgFunction; 032 033 public FrameInvoker() { 034 } 035 036 /** 037 * 设置获取beanfunction 038 * 039 * @param function 040 * @param <M> 041 */ 042 public static <M> void setBeanFunction(Function<Class<M>, M> function) { 043 frameInvoker.beanFunction = function; 044 } 045 046 public static <M> M getBean(Class<M> tClass) { 047 try { 048 return (M) frameInvoker.beanFunction.apply(tClass); 049 } catch (Exception e) { 050 return null; 051 } 052 } 053 054 /** 055 * 设置获取配置function 056 * 057 * @param function 058 */ 059 public static void setCfgFunction(Function<String, String> function) { 060 frameInvoker.cfgFunction = function; 061 } 062 063 public static String getCfg(String key) { 064 try { 065 return (String) frameInvoker.cfgFunction.apply(key); 066 } catch (Exception e) { 067 return null; 068 } 069 } 070 071}