成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

工具類如何獲取到 Spring 容器中的 Bean?

開發(fā) 開發(fā)工具
當(dāng)然這只是一個理論,在實際開發(fā)中,我們往往要用到 Spring 容器為我們提供的諸多資源,例如想要獲取到容器中的配置、獲取到容器中的 Bean 等等。在這種情況下,就需要 Spring 容器中的 Bean 真正的意識到 Spring 容器的存在,才能要到這些東西,那么如何讓一個 Bean 意識到 Spring 容器的存在呢?

1. Aware 接口

小伙伴們知道,Spring 容器最大的特點在于所有的 Bean 對于 Spring 容器的存在是沒有意識的,因此我們常說理論上你可以無縫將 Spring 容器切換為其他容器(然而在現(xiàn)實世界中,我們其實沒有這樣的選擇,除了 Spring 容器,難道還有更好用的?)。

當(dāng)然這只是一個理論,在實際開發(fā)中,我們往往要用到 Spring 容器為我們提供的諸多資源,例如想要獲取到容器中的配置、獲取到容器中的 Bean 等等。在這種情況下,就需要 Spring 容器中的 Bean 真正的意識到 Spring 容器的存在,才能要到這些東西,那么如何讓一個 Bean 意識到 Spring 容器的存在呢?

這就依賴于 Spring 容器給我們提供的各種 Aware 接口了。

/**
* A marker superinterface indicating that a bean is eligible to be notified by the
* Spring container of a particular framework object through a callback-style method.
* The actual method signature is determined by individual subinterfaces but should
* typically consist of just one void-returning method that accepts a single argument.
*
* <p>Note that merely implementing {@link Aware} provides no default functionality.
* Rather, processing must be done explicitly, for example in a
* {@link org.springframework.beans.factory.config.BeanPostProcessor}.
* Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
* for an example of processing specific {@code *Aware} interface callbacks.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
*/
public interface Aware {

}

從這個接口的注釋中,我們也能大概看出來,這個接口的子類,主要是提供了一些只有一個參數(shù)的 set 方法,通過這些方法可以讓 Spring 容器感知到某一件事情。

Aware 的實現(xiàn)有很多,大的方向來說主要有如下一些:

圖片

每一個 Aware 的作用如下:

  • ApplicationEventPublisherAware:實現(xiàn)該接口的對象可以獲取事件發(fā)布的能力。
  • ServletContextAware:實現(xiàn)該接口的對象可以獲取到 ServletContext 對象。
  • MessageSourceAware:實現(xiàn)該接口的對象可以獲取到 MessageSource 對象,MessageSource 支持多消息源,主要用于主要用于國際化。
  • ResourceLoaderAware:實現(xiàn)該接口的對象可以獲取到一個 ResourceLoader,Spring ResourceLoader 則為我們提供了一個統(tǒng)一的 getResource() 方法來通過資源路徑檢索外部資源,例如文本文件、XML 文件、屬性文件或圖像文件等。
  • ApplicationStartupAware:實現(xiàn)該接口的對象可以獲取到一個 ApplicationStartup 對象,這個比較新,是 Spring 5.3 中新推出的,通過 ApplicationStartup 可以標(biāo)記應(yīng)用程序啟動期間的步驟,并收集有關(guān)執(zhí)行上下文或其處理時間的數(shù)據(jù)。
  • NotificationPublisherAware:實現(xiàn)該接的對象可以獲取到一個 NotificationPublisher 對象,通過該對象可以實現(xiàn)通知的發(fā)送。
  • EnvironmentAware:實現(xiàn)該接口的對象可以獲取到一個 Environment 對象,通過 Environment 可以獲取到容器的環(huán)境信息。
  • BeanFactoryAware:實現(xiàn)該接口的對象可以獲取到一個 BeanFactory 對象,通過 BeanFactory 可以完成 Bean 的查詢等操作。
  • ImportAware:實現(xiàn)該接口的對象可以獲取到一個 AnnotationMetadata 對象,ImportAware 接口是需要和 @Import 注解一起使用的。在 @Import 作為元注解使用時,通過 @Import 導(dǎo)入的配置類如果實現(xiàn)了 ImportAware 接口就可以獲取到導(dǎo)入該配置類接口的數(shù)據(jù)配置。
  • EmbeddedValueResolverAware:實現(xiàn)該接口的對象可以獲取到一個 StringValueResolver 對象,通過 StringValueResolver 對象,可以讀取到 Spring 容器中的 properties 配置的值(YAML 配置也可以)。
  • ServletConfigAware:實現(xiàn)該接口的對象可以獲取到一個 ServletConfig 對象,不過這個似乎沒什么用,我們很少自己去配置 ServletConfig。
  • LoadTimeWeaverAware:實現(xiàn)該接口的對象可以獲取到一個 LoadTimeWeaver 對象,通過該對象可以獲取加載 Spring Bean 時織入的第三方模塊,如 AspectJ 等。
  • BeanClassLoaderAware:實現(xiàn)該接口的對象可以獲取到一個 ClassLoader 對象,ClassLoader 能干嘛不需要我多說了吧。
  • BeanNameAware:實現(xiàn)該接口的對象可以獲取到一個當(dāng)前 Bean 的名稱。
  • ApplicationContextAware:實現(xiàn)該接口的對象可以獲取到一個 ApplicationContext 對象,通過 ApplicationContext 可以獲取容器中的 Bean、環(huán)境等信息。

這是 Spring 中提供的一堆 Aware。

接下來松哥隨便寫個例子大家來看下 Aware 的用法。

2. BeanFactoryAware

實現(xiàn)該接口的對象可以獲取到一個 BeanFactory 對象,通過 BeanFactory 可以完成 Bean 的查詢等操作。這算是一個比較常見的 Aware 了,我們一起來看下。

這里為了省事,我就在 Spring Boot 中來和大家演示。

首先我們來定義一個簡單的 UserService:

@Service
public class UserService {
public void hello() {
System.out.println("hello javaboy!");
}
}

然后提供一個工具類:

@Component
public class BeanUtils implements BeanFactoryAware {
private static BeanFactory beanFactory = null;

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
BeanUtils.beanFactory = beanFactory;
}

public static <T> T getBean(String beanName) {
return (T) beanFactory.getBean(beanName);
}
}

有了這個工具類,接下來我們就可以在一個非 Spring 管理的 Bean 中,隨時隨地的查詢 Bean 了,像下面這樣:

UserService userService = BeanUtils.getBean("userService");
userService.hello();

3. TienChin 項目實踐

為什么會有今天這篇文章呢?主要是在松哥最近做的 TienChin 項目中,有一個地方涉及到這塊知識點了,但是有的小伙伴不熟悉,因此就拎出來和大家梳理下。

在 TienChin 項目中,在記錄日志的時候,因為日志是一個延遲任務(wù),所以提前準(zhǔn)備好了相關(guān)的 Bean 已經(jīng)注冊到 Spring 容器中了,像下面這樣:

@Configuration
public class ThreadPoolConfig {

/**
* 執(zhí)行周期性或定時任務(wù)
*/
@Bean(name = "scheduledExecutorService")
protected ScheduledExecutorService scheduledExecutorService() {
return new ScheduledThreadPoolExecutor(corePoolSize,
new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(),
new ThreadPoolExecutor.CallerRunsPolicy()) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
Threads.printException(r, t);
}
};
}
}
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
/**
* Spring應(yīng)用上下文環(huán)境
*/
private static ConfigurableListableBeanFactory beanFactory;

private static ApplicationContext applicationContext;

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
SpringUtils.beanFactory = beanFactory;
}

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

/**
* 獲取對象
*
* @param name
* @return Object 一個以所給名字注冊的bean的實例
* @throws org.springframework.beans.BeansException
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
return (T) beanFactory.getBean(name);
}

/**
* 獲取類型為requiredType的對象
*
* @param clz
* @return
* @throws org.springframework.beans.BeansException
*/
public static <T> T getBean(Class<T> clz) throws BeansException {
T result = (T) beanFactory.getBean(clz);
return result;
}
}

而寫日志的異步任務(wù)工具類,并非一個容器,所以要通過這個工具類獲取相應(yīng)的 Bean,如下:

public class AsyncManager {
/**
* 操作延遲10毫秒
*/
private final int OPERATE_DELAY_TIME = 10;

/**
* 異步操作任務(wù)調(diào)度線程池
*/
private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");

/**
* 單例模式
*/
private AsyncManager() {
}

private static AsyncManager me = new AsyncManager();

public static AsyncManager me() {
return me;
}

/**
* 執(zhí)行任務(wù)
*
* @param task 任務(wù)
*/
public void execute(TimerTask task) {
executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
}
}

有了 SpringUtils 我們就可以在一個非 Spring 容器所管理的 Bean 中,獲取到 Spring 容器中的 Bean 了。

責(zé)任編輯:武曉燕 來源: 江南一點雨
相關(guān)推薦

2023-01-13 07:41:20

BeanSpring容器

2023-11-03 08:19:18

SpringBean容器

2009-06-17 17:20:14

BeanFactorySpring

2024-05-29 08:19:03

2022-12-27 08:12:27

IOC容器Bean

2009-06-17 17:04:37

BeanFactorySpring

2024-02-23 10:33:34

SpringBean容器

2023-07-26 00:43:31

2022-07-20 07:32:46

Prototypevalue?容器

2011-03-18 09:27:00

Spring

2022-07-21 09:31:58

Actuator密碼框架

2021-03-08 08:40:25

Spring Bean 創(chuàng)建單例對象

2022-04-27 08:19:53

Docker容器Linux

2024-05-28 07:55:31

SpringBean用域

2022-01-10 09:43:55

CCleanerWindows 11微軟商店

2021-04-29 07:18:21

Spring IOC容器單例

2021-05-11 07:42:59

BeanSpring屬性

2023-07-29 22:11:58

Spring容器Component

2009-01-30 09:12:11

SpringApplicationJavaWeb

2011-04-02 15:25:41

Spring
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 国产不卡视频 | 亚洲一区亚洲二区 | 一a级片| 成人伊人网 | 国产欧美一区二区三区日本久久久 | 丝袜美腿av | 国产精品片 | 欧美9999| 亚洲免费在线观看 | 国产99视频精品免费视频7 | 亚洲精品一区在线 | 国产一区二区电影 | 天天插天天操 | 一级毛片免费 | 欧美日韩不卡合集视频 | 日韩欧美视频在线 | 粉嫩国产精品一区二区在线观看 | 久久免费福利 | 久久亚| 成人国产精品色哟哟 | 精品一区二区三区四区五区 | 亚洲国产成人精品女人久久久 | 日本一本在线 | 黄色a三级 | 青青草视频免费观看 | 久久九九免费 | 欧美一区二区三区视频 | 国产精品资源在线观看 | 91精品久久久 | 欧美精品久久久久久 | 91亚洲精品国偷拍自产在线观看 | 99亚洲国产精品 | 日韩在线观看中文字幕 | 成人久久18免费网站图片 | 久久精品中文字幕 | 欧美精品综合 | 国产精品视频播放 | 久久久久久高清 | 国产第二页 | 亚洲成人网在线播放 | 国产毛片久久久久久久久春天 |