IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

【spring源码分析】IOC容器初始化——查漏补缺(一)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

前言:在【spring源码分析】IOC容器初始化(十一)中提到了初始化bean的三个步骤:

  • 激活Aware方法。
  • 后置处理器应用(before/after)。
  • 激活自定义的init方法。

这里我们就来看下Spring是如何激活Aware方法的。


Aware是什么

108_1.png

Aware是一个空接口,包路径为:org.springframework.beans.factory.Aware,它具有标识作用,实现了该接口的bean具有被Spring容器通知的能力,通知的方式采用回调的方式。

由于Aware是一个空接口,实际的方法签名由各个子类来确定,其该接口通常只会有一个接收单参数的set方法,该set方法的命名方式为set+去掉接口中的Aware后缀,即:setxxx(),那Spring中是如何处理实现Aware接口的bean的呢。在invokeAwareMethods方法中可以看到如下代码:

 // AbstractAutowireCapableBeanFactory
 private void invokeAwareMethods(final String beanName, final Object bean) {
         if (bean instanceof Aware) {
             // BeanNameAware
             if (bean instanceof BeanNameAware) {
                 ((BeanNameAware) bean).setBeanName(beanName);
             }
             // BeanClassLoaderAware
             if (bean instanceof BeanClassLoaderAware) {
                 ClassLoader bcl = getBeanClassLoader();
                 if (bcl != null) {
                     ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
                 }
             }
             // BeanFactoryAware
             if (bean instanceof BeanFactoryAware) {
                 ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
             }
         }
     }

分析:

首先判断bean是否实现了Aware接口,如果是则调用实例的setXXX()方法给实例设置xxx属性,在invokeAwareMethods方法中,主要处理BeanNameAware、BeanClassLoaderAware、BeanFactoryAware三种接口,也从侧面证实了Spring只会处理实现了这三种接口中任意一个的bean实例。

Aware示例演示

下面就演示Spring是如何处理实现这三个接口的bean的:

 public class UserDefinedAware implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware {

     private String name;
     private ClassLoader classLoader;
     private BeanFactory beanFactory;

     @Override
     public void setBeanName(String name) {
         System.out.println("调用了 BeanNameAware 的 setBeanName方法");
         this.name = name;
     }

     @Override
     public void setBeanClassLoader(ClassLoader classLoader) {
         System.out.println("调用了 BeanClassLoader 的 setBeanClassLoader 方法");
         this.classLoader = classLoader;
     }

     @Override
     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
         System.out.println("调用了 BeanFactoryAware 的 setBeanFactory 方法");
         this.beanFactory = beanFactory;
     }

     public void showMsg() {
         System.out.println("beanName=" + this.name);
         System.out.println("classLoader=" + this.classLoader.getClass());
         System.out.println("是否为单例=" + beanFactory.isSingleton(this.name));
     }
 }

测试方法如下:

     /**
      * Aware接口演示
      */
     @Test
     public void awareTest() {
         ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/dev/config/aware/aware.xml");
         UserDefinedAware userDefinedAware = context.getBean(UserDefinedAware.class);
         userDefinedAware.showMsg();
     }

输出结果如下:

108_2.png

Aware总结

从以上示例基本上可以了解 Aware接口 真正的含义:感知,其实是Spring容器在初始化主动检测当前bean是否实现了Aware接口,如果实现了则回调其set方法将相应的参数设置给该bean,这个时候该bean就从Spring容器中取得了相应的资源。所以Aware接口的作用就是让实现该接口的bean能从Spring容器中取得相应的资源。这里如name、ClassLoader、beanFactory。

下面列出一些常用的Aware接口,便于日后查询:

  • LoadTimeWeaverAware:加载Spring Bean时织入第三方模块,如AspectJ
  • BeanClassLoaderAware:加载Spring Bean的类加载器
  • BootstrapContextAware:资源适配器BootstrapContext,如JCA,CCI
  • ResourceLoaderAware:底层访问资源的加载器
  • BeanFactoryAware:声明BeanFactory
  • PortletConfigAware:PortletConfig
  • PortletContextAware:PortletContext
  • ServletConfigAware:ServletConfig
  • ServletContextAware:ServletContext
  • MessageSourceAware:国际化
  • ApplicationEventPublisherAware:应用事件
  • NotificationPublisherAware:JMX通知
  • BeanNameAware:获取Spring Bean的名字

by Shawn Chen,2019.04.29,晚上。

出处:https://www.cnblogs.com/developer_chan/category/1347173.html

文章永久链接:https://tech.souyunku.com/?p=13692


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(99) 打赏



未经允许不得转载:搜云库技术团队 » 【spring源码分析】IOC容器初始化——查漏补缺(一)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367