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

Springboot如何通過自定義工具類獲取bean

 更新時間:2021年09月25日 10:49:34   作者:Zero .  
這篇文章主要介紹了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對象來執(zhí)行sql語句。而直接使用@Autowired無法進行注入。

2.使用環(huán)境

spring boot 2.0.3

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

在applicationContext對象中可以獲取到所有的bean

第一步:準備ApplicationContextAware的實現(xiàn)類,用于獲取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;

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

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

5.方法三:在spring Boot 啟動時創(chuàng)建工具類自身的靜態(tài)對象

在本質上,同方法二

第一步:當前類添加@Component注解

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

public static AuthInterceptor authInterceptor;

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

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

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

望奎县| 甘洛县| 且末县| 浪卡子县| 镇坪县| 云和县| 乃东县| 扶沟县| 白水县| 淄博市| 海口市| 金寨县| 安溪县| 新民市| 手游| 兰西县| 广灵县| 普安县| 明星| 寿阳县| 鸡东县| 合阳县| 石狮市| 永宁县| 巧家县| 措勤县| 新巴尔虎右旗| 体育| 莱芜市| 遂平县| 兴海县| 武夷山市| 张家川| 泽库县| 法库县| 阳朔县| 乐陵市| 隆尧县| 芦溪县| 普安县| 香港|