/* * Copyright 2025 mingliqiye * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile SpringBeanUtil.java * LastUpdate 2025-09-09 08:37:33 * UpdateUser MingLiPro */ package com.mingliqiye.utils.bean.springboot; import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * Spring Bean工具类 * 实现ApplicationContextAware接口,并加入Component注解,让spring扫描到该bean * 该类用于在普通Java类中注入bean,普通Java类中用@Autowired是无法注入bean的 *
* 需要放入扫描类中
*
* @author MingLiPro
*/
@Component
public class SpringBeanUtil implements ApplicationContextAware {
public static final String PACKAGE_NAME =
"com.mingliqiye.utils.bean.springboot";
/**
* 获取applicationContext
*/
@Getter
private static ApplicationContext applicationContext;
/**
* 通过bean名称获取Bean实例
*
* @param name bean名称
* @return bean实例对象
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过bean类型获取Bean实例
*
* @param clazz bean的Class类型
* @param