首页 > 其他分享 >Spring 高级 Aware 接口及 InitializingBean 接口

Spring 高级 Aware 接口及 InitializingBean 接口

时间:2022-08-28 14:33:28浏览次数:54  
标签:InitializingBean Spring springframework bean 接口 org import public

一、Aware

1、Aware 接口的作用

Aware 接口提供了一种【内置】 的注入手段,例如

  • BeanNameAware 注入 bean 的名字
  • BeanFactoryAware 注入 BeanFactory 容器
  • ApplicationContextAware 注入 ApplicationContext 容器
  • EmbeddedValueResolverAware 注入 ${} 解析器

2、示范

package com.mangoubiubiu.show.a06;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * 在Bean创建过程中,初始化之前,就会回调BeanNameAware  接口 将Bean的名字传过来
 */
@Slf4j
public class MyBean implements BeanNameAware , ApplicationContextAware {
    @Override
    public void setBeanName(String s) {
     log.info("当前bean:{},名字叫{}",this,s);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("当前bean:{},容器是{}",this,applicationContext);
    }
}
package com.mangoubiubiu.show.a06;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.GenericApplicationContext;

@Slf4j
public class A06Application {


    public static void main(String[] args) {

       /*
        Aware 接口提供了一种【内置】 的注入手段,例如
                * BeanNameAware 注入 bean 的名字
                * BeanFactoryAware 注入 BeanFactory 容器
                * ApplicationContextAware 注入 ApplicationContext 容器
                * EmbeddedValueResolverAware 注入 ${} 解析器
        */



        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("myBean",MyBean.class);
        context.refresh();//初始化容器
        context.close();//关闭容器



    }

}

二、InitializingBean

1 InitializingBean 接口的作用

  1. InitializingBean 接口提供了一种【内置】的初始化手段
  2. 对比
    • 内置的注入和初始化不受扩展功能的影响,总会被执行
    • 而扩展功能受某些情况影响可能会失效
    • 因此 Spring 框架内部的类常用内置注入和初始化

2、示范

package com.mangoubiubiu.show.a06;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * 在Bean创建过程中,初始化之前,就会回调BeanNameAware  接口 将Bean的名字传过来
 */
@Slf4j
public class MyBean implements BeanNameAware , ApplicationContextAware, InitializingBean {
    @Override
    public void setBeanName(String s) {
     log.info("当前bean:{},名字叫{}",this,s);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("当前bean:{},容器是{}",this,applicationContext);
    }

    /**
     * 先去回调Bean的Aware 接口 然后再去执行的是InitializingBean的初始化方法
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("当前bean:{},初始化",this);
    }
}

三、面试考点(重点!!!) 

问题一:Aware 接口的以下功能都能用@Autowired就能实现,为啥还要用Aware 接口呢

  • BeanFactoryAware 注入 BeanFactory 容器
  • ApplicationContextAware 注入 ApplicationContext 容器
  • EmbeddedValueResolverAware 注入 ${} 解析器

答:1、@Autiwired的解析需要用到bean后处理器,属于扩展功能。

       2、而Aware 接口属于内置功能,不加任何扩展,Spring就能识别

         某些情况下,扩展功能会失效,而内置功能不会失效

示范

package com.mangoubiubiu.show.a06;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.PostConstruct;


/**
 * 在Bean创建过程中,初始化之前,就会回调BeanNameAware  接口 将Bean的名字传过来
 */
@Slf4j
public class MyBean implements BeanNameAware , ApplicationContextAware, InitializingBean {
    @Override
    public void setBeanName(String s) {
     log.info("当前bean:{},名字叫{}",this,s);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("当前bean:{},容器是{}",this,applicationContext);
    }

    /**
     * 先去回调Bean的Aware 接口 然后再去执行的是InitializingBean的初始化方法
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("当前bean:{},初始化",this);
    }

    @Autowired
    public void addAutowired(ApplicationContext applicationContext){
        log.info("使用 @Autowired 注入 当前bean:{},容器是{}",this,applicationContext);
    }

    @PostConstruct
    public void init(){
        log.info("使用 @PostConstruct 当前bean:{},初始化",this);

    }
}

 

 

 

标签:InitializingBean,Spring,springframework,bean,接口,org,import,public
From: https://www.cnblogs.com/mangoubiubiu/p/16632729.html

相关文章

  • Referenced file contains errors (http://www.springframework.org/schema/beans/spr
    Referencedfilecontainserrors(http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)._faihtua的博客-CSDN博客 https://blog.csdn.net/faihtua/arti......
  • Spring 高级 工厂后处理器模拟实现-Mapper
    一、源方式自动注入packagecom.mangoubiubiu.show.a05;importcom.alibaba.druid.pool.DruidDataSource;importcom.mangoubiubiu.show.a05.component.Bean2;impor......
  • 集合.List子接口
    特点:有序、有下标、元素可以重复方法:voidadd(intindex,Objecto);在index位置插入对象obooleanaddAll(intindex,Collectionc);将一个集合中的元素添加到此元素中......
  • Spring源码-自定义标签
    一、新建实体类publicclassUserimplementsSerializable{privateStringid;privateStringname;privateIntegerage;publicStringgetId(){ return......
  • spring源码具体细节 super setConfigLocations
      1首先先调用super父类构造方法 classPathXmlApplicaitonContext 初始化成员属性  依然掉父类构造方法 调用父类 资源处理器 当前系统需要运行所......
  • Spring源码01:环境搭建
    写在开始:这个系列会陆续更新我学习Spring源码的一些笔记与自己的理解,由于本人水平有限,难免对有些知识理解不到位,亦或者手误导致文中有不正确的地方,欢迎各位指正与探讨。......
  • Spring(二)-生命周期 + 自动装配(xml) +自动装配(注解)
    1、生命周期**Spring容器的bean**的生命周期;1.1默认生命周期1.1.1生命周期调用构造方法,创建实例对象;set方法,给实例对象赋值;init初始化方法初始化对象;(手......
  • Spring中的SPI机制
    前言在面向对象编程领域中,六大原则之一的依赖倒置原则提到的原则规定:高层次的模块不应该依赖于低层次的模块,两者都应该依赖于抽象接口;抽象接口不应该依赖于具体实现,而......
  • 理解Spring Security和实现动态授权
    一、SpringSecurity架构SpringSecurity是基于SpringAOP和Servlet过滤器的安全框架,提供全面的安全性解决方案。SpringSecurity核心功能包括用户认证(Authenticati......
  • Cannot resolve org.springframework.cloud:spring-cloud-starter-netflix-eureka-ser
    Cannotresolveorg.springframework.cloud:spring-cloud-starter-netflix-eureka-server:unknown前言:启动eureka项目,发现右侧maven中的项目dependencies报红,reimport也......