温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot怎么通过自定义工具类获取bean

发布时间:2021-09-26 09:55:21 来源:亿速云 阅读:232 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“Springboot怎么通过自定义工具类获取bean”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Springboot怎么通过自定义工具类获取bean”这篇文章吧。

Springboot 自定义工具类获取bean

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: zp
 * @Date: 2021/03/26/13:32
 * @Description: 通过beanFactory获取spring管理的bean对象工具类
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;
 
    // springboot加载完成后会把beanfactory作为参数传给次方法,然后我们可以把工厂赋值给context。
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    // 通过context获取bean
    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }
}

在工具类注入bean的三种方式

1. 需求/目的

比如:在进行使用HandlerInterceptorAdapter拦截器时,需要访问数据库来判断是否拦截请求,这时就需要在拦截器的判断类中注入Dao或Service对象来执行sql语句。而直接使用@Autowired无法进行注入。

2.使用环境

spring boot 2.0.3

3.方法一:获取ApplicationContext上下文

在applicationContext对象中可以获取到所有的bean

第一步:准备ApplicationContextAware的实现类,用于获取applicationContext对象

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.authstr.ff.utils.exception.Assert;
@Component
public class SpringUtils implements ApplicationContextAware {
    private static Log log = LogFactory.getLog(SpringUtils.class);
    private static ApplicationContext applicationContext;
    public   void setApplicationContext(ApplicationContext applicationContext) {
        SpringUtils.applicationContext = applicationContext;
    }
    private static ApplicationContext getContext() {
        return applicationContext;
    }
    public static Object getBean(String beanId) {
        return SpringUtils.getBean(Object.class, beanId);
    }
    public static <T> T getBean(Class<T> clazz, String beanId) throws ClassCastException {
        ApplicationContext context = SpringUtils.getContext();
        Assert.isTrue(StringUtils.hasText(beanId), "beanId must not null!",true);
        boolean a=context.containsBean(beanId);
        Assert.isTrue(context.containsBean(beanId), "beanId :[" + beanId + "] is not exist!",true);
        Object bean = null;
        bean = context.getBean(beanId);
        return (T)bean;
    }
}

这是已经写好的工具类,可以根据bean的id获取对应的bean

第二步: 对要获取的bean设置id

如:

@Component("basicDaoImpl")
public class BasicDaoImpl extends AbstractDao implements BasicDao

第三步: 在要使用的类中写一个方便调用的方法

public BasicDaoImpl getBasicDaoImpl (){
        return SpringUtils.getBean(BasicDaoImpl .class, "basicDaoImpl");
    }

4.方法二:将工具类的对象也添加为bean

第一步:当前类添加@Component注解

第二步:对要获取的对象使用@Autowired 注解

@Autowired 
private BasicDaoImpl basicDaoImpl;

第三步:在创建该工具类的地方,这样定义

@Bean
  public AuthInterceptor authInterceptor(){
        return new AuthInterceptor();
    }

5.方法三:在spring Boot 启动时创建工具类自身的静态对象

在本质上,同方法二

第一步:当前类添加@Component注解

第二步:在工具类创建一个自身的静态对象

public static AuthInterceptor authInterceptor;

第三步:使用@PostConstruct注解,在springboot加载时执行该方法

  @PostConstruct
    public void init() {
        authInterceptor= this;
        AuthInterceptor .authInterceptor= this.authInterceptor;
    }

以上是“Springboot怎么通过自定义工具类获取bean”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI