006 know BeanNameAware

  Spring look at the life cycle, we should note that Spring Aware, Aware of what is in this article, is to specify the three parts, Spring life cycle [this will continue to be described later, here it is] an outline, then DESCRIPTION Spring Aware, finally BeanNameAware be described as an example.

A: Life Cycle

1. Fig.

  

 

2. Description

  1. In the Spring Bean  Ready for Useoriginated before, of course, is to call the constructor, so Constructor is no doubt the first step in creating a Spring Bean
  2. Setter injection method by complete dependency, SDI (Setter Dependency Injection)
  3. Dependency injection once completed, BeanNameAware.setBeanName() will be called, it sets the name of the bean in the Bean Factory
  4. Next call  BeanClassLoaderAware.setBeanClassLoader() , a class loader for the bean instance, we know that all classes are to be loaded into context, the loading mechanism on classes by the class loader / parent delegation model (manufacturers love to ask interview questions) content will follow to come out, let your thorough understanding
  5. Then  BeanFactoryAware.setBeanFactory() it will be called to provide factory bean instance in its possession

 

Two: Aware Description

1. Functional Description

  Spring 的依赖注入最大亮点就是所有的 Bean 对 Spring 容器的存在是没有意识的,拿 [Spring Bean 生命周期之“我从哪里来”?]() 文章中“小学生入少先队”为例子说明,小学生还是那个小学生,加入少先队还是加入共青团只不过规则不一样罢了
  但是在实际项目中,我们不可避免的要用到 Spring 容器本身提供的资源(难免要有事情需要少先队组织的帮助),这时候要让 Bean 主动意识到 Spring 容器的存在,才能调用 Spring 所提供的资源,这就是 Spring Aware. 其实 Spring Aware 是 Spring 设计为框架内部使用的,若使用了,你的 Bean 将会和 Spring 框架耦合,所以自己不单独使用,但是在读框架源码时希望你不再模糊.

 

2.常用的Aware

Aware子接口 描述
BeanNameAware 获取容器中 Bean 的名称
BeanFactoryAware 获取当前 BeanFactory ,这样可以调用容器的服务
ApplicationContextAware 同上,在BeanFactory 和 ApplicationContext 的区别 中已明确说明
MessageSourceAware 获取 Message Source 相关文本信息
ApplicationEventPublisherAware 发布事件
ResourceLoaderAware 获取资源加载器,这样获取外部资源文件

 

三:BeanNameAware

1.程序结构

  

 

2.自定义Aware,同时,注册bean

 1 package com.jun.useTestPack.aware.beanName;
 2 
 3 import org.springframework.beans.factory.BeanNameAware;
 4 import org.springframework.stereotype.Component;
 5 
 6 /**
 7  * 自定义BeanNameAware
 8  */
 9 @Component(value = "testBeanName2")
10 public class MyBeanName implements BeanNameAware {
11     @Override
12     public void setBeanName(String name) {
13         System.out.println("MyBeanName-setBeanName:"+name);
14     }
15 }

 

3.测试

  启动spring boot进行测试。

 1 package com.jun.useTestPack.aware.beanName;
 2 
 3 import com.jun.SpringBootApplicationTest;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 7 
 8 @SpringBootApplication
 9 public class ApplicationDemo {
10     public static void main(String[] args) {
11         SpringApplication.run(SpringBootApplicationTest.class,args);
12     }
13 }

 

4.效果

  

 

4.结论

  在生命周期中,会调用BeanNameAware接口的实现类中的setBeanName方法。

  在这个方法中,说明可以拿到BeanName。

  

 

Guess you like

Origin www.cnblogs.com/juncaoit/p/11227001.html