Aware接口是一个标志性接口,继承此接口的接口xxxAware的实现类,在容器创建完成后,会回调实现方法,下面举例:

1. 有很多xxxAware接口,下面举两个例子

  1. /**
  2. * description: 将实现xxxAware接口的Bean注册到IOC容器中的时候,会将xxxAware的实现方法进行回调操作
  3. *
  4. * @author 70KG
  5. * @date 2018/12/17
  6. */
  7. @Component
  8. public class MyAware implements ApplicationContextAware, BeanNameAware {
  9.  
  10. private ApplicationContext applicationContext;
  11.  
  12. private String beanName;
  13.  
  14. @Override
  15. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  16. this.applicationContext = applicationContext;
  17. Cat cat = (Cat) applicationContext.getBean("cat");
  18. System.out.println("------->" + cat);
  19. }
  20.  
  21. @Override
  22. public void setBeanName(String name) {
  23. this.beanName = name;
  24. System.out.println("------->" + beanName);
  25. }
  26. }

2. 配置类

  1. /**
  2. * description
  3. *
  4. * @author 70KG
  5. * @date 2018/12/17
  6. */
  7. @Configuration
  8. public class MyConfig {
  9.  
  10. @Bean
  11. public Cat cat() {
  12. return new Cat();
  13. }
  14.  
  15. @Bean
  16. public Dog dog() {
  17. return new Dog();
  18. }
  19.  
  20. @Bean
  21. public MyAware myAware() {
  22. return new MyAware();
  23. }
  24.  
  25. }

3. 测试类

  1. /**
  2. * description
  3. *
  4. * @author 70KG
  5. * @date 2018/12/17
  6. */
  7. public class Test01 {
  8. public static void main(String[] args) {
  9. AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class);
  10. Cat cat = (Cat) app.getBean("cat");
  11. System.out.println("------->" + cat);
  12. }
  13. }

4. 结果

  1. ------->myAware
  2. ------->com.nmys.story.springCore.springioc.importBean.Cat@78b66d36
  3. ------->com.nmys.story.springCore.springioc.importBean.Cat@78b66d36

Spring源码窥探之:xxxAware接口的更多相关文章

  1. Spring源码解析 - AbstractBeanFactory 实现接口与父类分析

    我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ...

  2. spring源码系列(二):IOC接口设计分析

    这里主要对springIOC接口体系进行简单的概述和分析,具体每个接口详细分析在下面目录: 参考内容: <Spring技术内幕:深入解析 Spring架构与设计原理> 和 <Spri ...

  3. 【spring源码分析】BeanDefinitionRegistryPostProcessor接口可自定义bean加入IOC

    自定义BeanDefinitionRegistryPostProcessor BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcesso ...

  4. Spring源码窥探之:注解方式的AOP原理

    AOP入口代码分析 通过注解的方式来实现AOP1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容 ...

  5. Spring源码窥探之:Spring AOP初步

    AOP(Aspect Oriented Programming):即我们常说的面向切面编程. 什么是AOP?AOP是在我们原来写的代码的基础上,进行一定的包装,比如在方法执行前.方法返回后.方法抛出异 ...

  6. Spring源码窥探之:扩展原理BeanDefinitionRegistryPostProcessor

    BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor,其中有两个接口,postProcessBeanDefinitionRegi ...

  7. Spring源码窥探之:声明式事务

    1. 导入驱动,连接池,jdbc和AOP的依赖 <!-- c3p0数据库连接池 --> <dependency> <groupId>c3p0</groupId ...

  8. Spring源码窥探之:AOP注解

    AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么.在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@A ...

  9. Spring源码窥探之:@Profile

    Spring为我们提供的多环境启动 1. 配置类,注入三个不同环境的数据源,并加上注解 /** * description: 以下准备了三套不同环境的数据源 * * @author 70KG * @d ...

随机推荐

  1. Saltack 针对用户进行权限控制

    一. client_acl 配置使用 1.1 client_acl概述 开启对系统上非root的系统用户在master上执行特殊的模块,这些模块名可以使用正则表达式进行表示,不能指定对那些minion ...

  2. 8、2 es数据库的使用

    1.注意问题.es和redis同时使用会报错 解决: package com.bw; import org.springframework.beans.factory.InitializingBean ...

  3. Hbase面试题

    hbase的特点 )hbase适合存储海量数据,是一个分布式的,基于列式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. )hbase 适合存储半结构化或非结构化的数据,对于 ...

  4. day41——数值类型、完整性约束

    day41 数值类型 整数类型 有符号的设置 mysql> create table t1(id tinyint); # 默认有符号,即数字前有正负号 无符号的设置 mysql> crea ...

  5. php策略模式实现简单计算器

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  6. nginx 简单介绍

    Nginx同Apache一样都是一种WEB服务器.基于REST架构风格,以统一资源描述符(Uniform Resources Identifier)URI或者统一资源定位符(Uniform Resou ...

  7. 物流管理系统(SSM+vue+shiro)【前后台】

    一.简单介绍项目 该项目是属于毕业设计项目之一,有前台的用户下单.有司机进行接单.有管理员进行操作后台,直接进入主题 毕设.定制开发 联系QQ:761273133 登录主页: 手机号码+验证码登录 或 ...

  8. springboot 2.1.3.RELEASE添加filter,servlet源码学习

    Servlet规范中,通过ServeltContext来注册Filter.Servlet,这里分析Filter,Servlet是相同逻辑 springboot2.0中,我们通过 FilterRegis ...

  9. 关于Java无法解码(ajax编码 Java解码)

    今天遇到了一个非常奇~~~~~~葩的问题,无解! 一向前端碰到中文,请求都使用encodeURI(encodeURI("中文"))编码,然后后端使用URLDecoder.decod ...

  10. Dubbo的10种集群容错模式

    学习Dubbo源码的过程中,首先看到的是dubbo的集群容错模式,以下简单介绍10种集群容错模式 1.AvailableCluster 顾名思义,就是可用性优先,遍历所有的invokers,选择可用的 ...