最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot如何通過(guò)自定義工具類(lèi)獲取bean

 更新時(shí)間:2021年09月25日 10:49:34   作者:Zero .  
這篇文章主要介紹了Springboot通過(guò)自定義工具類(lèi)獲取bean方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Springboot 自定義工具類(lèi)獲取bean

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: zp
 * @Date: 2021/03/26/13:32
 * @Description: 通過(guò)beanFactory獲取spring管理的bean對(duì)象工具類(lèi)
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;
 
    // springboot加載完成后會(huì)把beanfactory作為參數(shù)傳給次方法,然后我們可以把工廠賦值給context。
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    // 通過(guò)context獲取bean
    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }
}

在工具類(lèi)注入bean的三種方式

1. 需求/目的

比如:在進(jìn)行使用HandlerInterceptorAdapter攔截器時(shí),需要訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)來(lái)判斷是否攔截請(qǐng)求,這時(shí)就需要在攔截器的判斷類(lèi)中注入Dao或Service對(duì)象來(lái)執(zhí)行sql語(yǔ)句。而直接使用@Autowired無(wú)法進(jìn)行注入。

2.使用環(huán)境

spring boot 2.0.3

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

在applicationContext對(duì)象中可以獲取到所有的bean

第一步:準(zhǔn)備ApplicationContextAware的實(shí)現(xiàn)類(lèi),用于獲取applicationContext對(duì)象

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;
    }
}

這是已經(jīng)寫(xiě)好的工具類(lèi),可以根據(jù)bean的id獲取對(duì)應(yīng)的bean

第二步: 對(duì)要獲取的bean設(shè)置id

如:

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

第三步: 在要使用的類(lèi)中寫(xiě)一個(gè)方便調(diào)用的方法

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

4.方法二:將工具類(lèi)的對(duì)象也添加為bean

第一步:當(dāng)前類(lèi)添加@Component注解

第二步:對(duì)要獲取的對(duì)象使用@Autowired 注解

@Autowired 
private BasicDaoImpl basicDaoImpl;

第三步:在創(chuàng)建該工具類(lèi)的地方,這樣定義

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

5.方法三:在spring Boot 啟動(dòng)時(shí)創(chuàng)建工具類(lèi)自身的靜態(tài)對(duì)象

在本質(zhì)上,同方法二

第一步:當(dāng)前類(lèi)添加@Component注解

第二步:在工具類(lèi)創(chuàng)建一個(gè)自身的靜態(tài)對(duì)象

public static AuthInterceptor authInterceptor;

第三步:使用@PostConstruct注解,在springboot加載時(shí)執(zhí)行該方法

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

万源市| 偏关县| 宣武区| 班玛县| 克什克腾旗| 商城县| 北碚区| 郓城县| 云阳县| 墨玉县| 孟村| 保德县| 万年县| 英吉沙县| 凤阳县| 惠水县| 仁怀市| 开封市| 泗水县| 平远县| 肇州县| 寻乌县| 平乐县| 卓尼县| 宣威市| 宜良县| 肥乡县| 阳西县| 绩溪县| 宁乡县| 长武县| 东海县| 宝应县| 扬中市| 云阳县| 正定县| 宣恩县| 政和县| 安龙县| 达日县| 亳州市|