Java通过类名字符串获取类实例,使用反射执行bean里面的方法

新建ApplicationContextGetBeanHelper类作为单独获取bean的类


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

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

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

   /**
     * 这里的className 可以只是所需类名的单独字符串,不需要加上包名路径
     * 也可以是数据库名称转化为驼峰之后的类名字符串,如sys_user_info为数据库名称,转化为驼峰命名字符串为SysUserInfo
     * 通过SysUserInfo获取与之相关的类,如果想获取其ServiceImpl业务实现类,则加上后缀ServiceImpl即可
     * @param className
     * @return
     * @throws BeansException
     * @throws IllegalArgumentException
     */
    public static Object getBean(String className) throws BeansException,IllegalArgumentException {
        if(className==null || className.length()<=0) {
            throw new IllegalArgumentException("className为空");
        }

        String beanName = null;
        if(className.length() > 1) {
            beanName = className.substring(0, 1).toLowerCase() + className.substring(1);
        } else {
            beanName = className.toLowerCase();
        }
        return applicationContext != null ? applicationContext.getBean(beanName) : null;
    }
}

在需要使用反射的地方,调用相关实例



import com.ttckj.common.utils.ApplicationContextGetBeanHelper;
import com.ttckj.common.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

/**
 * 需要处理的业务数据实现反射类
 */
@Service
@Slf4j
public class TestServiceImpl  {

    /**
     * @param tableName 数据表名称,如sys_user_info
     * @param userId 需要向执行目标方法传入的参数,可有可无
     */
    public void updateAfterDoSomething(String tableName,Long userId) {
        try {
            //将下划线的表名转化为驼峰命名字符串
                String className = CommonUtil.underLineToTuofeng(tableName)+"ServiceImpl";
                //获取bean
                Object object = ApplicationContextGetBeanHelper.getBean(className);
                //获取class
                Class clazz = object.getClass();
                //获取方法名称,并设置方法传入参数的类型
                Method bean = clazz.getMethod("queryInfo",Long.class);
                //传入参数,执行方法
                bean.invoke(object, userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}



/**
 * 需要处理的业务数据实现类
 */
@Service
@Slf4j
public class MyServiceImpl  {
	@Resource
    private TestServiceImpl  handlerService;

    /**
     * @param tableName 数据表名称,如sys_user_info
     * @param userId 需要向执行目标方法传入的参数,可有可无
     */
    public void businessDo(Long userId) {
        handlerService.updateAfterDoSomething("sys_user_info",userId);
    }

}

这时候就需要有SysUserInfoServiceImpl类来执行反射里面的queryInfo方法

import org.springframework.stereotype.Service;

@Service
public class SysUserInfoServiceImpl {
     public void  queryInfo(Long userId){
     	//这里处理想要处理的业务
         System.out.println("userId = " + userId);
     }
}

附上反射里面的CommonUtil.underLineToTuofeng(tableName)方法


import cn.hutool.core.util.StrUtil;


public class CommonUtil {
    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String underLineToTuofeng(String str) {
        return org.apache.commons.lang3.StringUtils.capitalize(StrUtil.toCamelCase(str));
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值