本文主要是介绍Spring底层原理学习笔记--第六讲--(Aware与InitializingBean接口及@Autowired失效分析),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Aware接口
- 1.Aware接口提供了一种【内置】的注入手段,可以注入BeanFactory,ApplicationContext
- 2.InitiazingBean接口提供了一种【内置】的初始化手段
- 3.内置的注入和初始化不收扩展功能的影响,总会被执行,因此Spring框架内部的类常用它们
- 4.实战:@Autowired失效分析
Aware与InitializingBean接口
A06Application.java
package com.lucifer.itheima.a06;import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.support.GenericApplicationContext;/*** Aware接口及InitializingBean接口*/
public class A06Application {public static void main(String[] args) {/*1.Aware接口用于注入一些与容器相关信息,例如a.BeanNameAware 注入bean的名字b.BeanFactoryAware 注入BeanFactory容器c.ApplicationContextAware注入ApplicationContext容器d.EmbeddedValueResolverAware ${}*/// 输出结果为
// 15:56:19.704 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@5bcab519 名字叫:myBean
// 15:56:19.706 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@5bcab519 容器是:org.springframework.context.support.GenericApplicationContext@5aaa6d82, started on Wed Nov 08 15:56:19 CST 2023
// 15:56:19.706 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@5bcab519初始化GenericApplicationContext context = new GenericApplicationContext();context.registerBean("myBean",MyBean.class);//不加这两句
// context.registerBean(AutowiredAnnotationBeanPostProcessor.class);
// context.registerBean(CommonAnnotationBeanPostProcessor.class);//的输出结果是
// 16:26:47.976 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@5bcab519 名字叫:myBean
// 16:26:47.979 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@5bcab519 容器是:org.springframework.context.support.GenericApplicationContext@5aaa6d82, started on Wed Nov 08 16:26:47 CST 2023
// 16:26:47.979 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@5bcab519初始化// 加上那两句(后处理器)的输出结果是
// 16:28:24.501 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@262b2c86 使用!Autowired 容器是:org.springframework.context.support.GenericApplicationContext@5aaa6d82, started on Wed Nov 08 16:28:24 CST 2023
// 16:28:24.501 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@262b2c86 名字叫:myBean
// 16:28:24.501 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@262b2c86 容器是:org.springframework.context.support.GenericApplicationContext@5aaa6d82, started on Wed Nov 08 16:28:24 CST 2023
// 16:28:24.501 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@262b2c86 使用@PostConstruct 初始化
// 16:28:24.501 [main] INFO com.lucifer.itheima.a06.MyBean - 当前bean com.lucifer.itheima.a06.MyBean@262b2c86初始化context.registerBean(AutowiredAnnotationBeanPostProcessor.class);context.registerBean(CommonAnnotationBeanPostProcessor.class);context.refresh();context.close();/*** 2.b、c、d的功能用!Autowired就能实现,为什么还要用Aware接口* 简单地说:* a.@Autowired的解析需要用到bean后处理器,属于扩展功能* b.而Aware接口属于内置功能,不加任何扩展,Spring就能识别* 某些情况下,扩展功能会实现,而内置功能不会生效** 问1:你会发现用Aware注入ApplicationContext成功,而@Autowired注入ApplicaitonContext失效*/}
}
MyBean.java
package com.lucifer.itheima.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;@Slf4j
public class MyBean implements BeanNameAware, ApplicationContextAware, InitializingBean {@Overridepublic void setBeanName(String name) {log.info("当前bean "+ this + " 名字叫:" + name);}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {log.info("当前bean " + this + " 容器是:" + applicationContext);}@Overridepublic void afterPropertiesSet() throws Exception {log.info("当前bean " + this + "初始化");}@Autowiredpublic void aaa(ApplicationContext applicationContext) {log.info("当前bean " + this + " 使用!Autowired 容器是:" + applicationContext);}@PostConstructpublic void init() {log.info("当前bean " + this + " 使用@PostConstruct 初始化");}
}
@Autowired失效分析
A06Application.java
package com.lucifer.itheima.a06;import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;/*** Aware接口及InitializingBean接口*/
public class A06Application {public static void main(String[] args) {/*1.Aware接口用于注入一些与容器相关信息,例如a.BeanNameAware 注入bean的名字b.BeanFactoryAware 注入BeanFactory容器c.ApplicationContextAware注入ApplicationContext容器d.EmbeddedValueResolverAware ${}*/GenericApplicationContext context = new GenericApplicationContext();
// context.registerBean("myBean",MyBean.class);
// context.registerBean("myConfig1",MyConfig1.class);context.registerBean("myConfig2",MyConfig2.class);context.registerBean(AutowiredAnnotationBeanPostProcessor.class); //能解析@Autowired注解context.registerBean(CommonAnnotationBeanPostProcessor.class); //能解析@PostConstruct注解context.registerBean(ConfigurationClassPostProcessor.class); //能解析@ComponentScan @Bean @Import @ImportResourcecontext.refresh(); //1.会到容器中找到所有beanFactory后处理器来执行 2.添加bean后处理器 3.初始化单例context.close();/*** 2.b、c、d的功能用!Autowired就能实现,为什么还要用Aware接口* 简单地说:* a.@Autowired的解析需要用到bean后处理器,属于扩展功能* b.而Aware接口属于内置功能,不加任何扩展,Spring就能识别* 某些情况下,扩展功能会实现,而内置功能不会生效** 问1:你会发现用Aware注入ApplicationContext成功,而@Autowired注入ApplicaitonContext失效*//***学到了什么* a.Aware接口提供了一种[内置]的注入手段,可以注入BeanFactory,ApplicationContext* b.InitializingBean接口提供了一种【内置】的初始化手段* c.内置的注入和初始化不受扩展功能的影响,总会被执行,因此Spring框架内的类常用它们*/}
}
MyConfig1.java
package com.lucifer.itheima.a06;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;@Configuration
@Slf4j
public class MyConfig1 {@Autowiredpublic void setApplicationContext(ApplicationContext applicationContext) {log.info("注入 ApplicationContext");}@PostConstructpublic void init() {log.info("初始化");}@Bean // beanFactory后处理器public BeanFactoryPostProcessor processor1() {return beanFactory -> {log.info("执行 processor1");};}}
MyConfig2.java
package com.lucifer.itheima.a06;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@Slf4j
public class MyConfig2 implements InitializingBean, ApplicationContextAware {@Overridepublic void afterPropertiesSet() throws Exception {log.info("初始化");}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {log.info("注入 ApplicationContext");}@Bean // beanFactory后处理器public BeanFactoryPostProcessor processor1() {return beanFactory -> {log.info("执行 processor1");};}
}
这篇关于Spring底层原理学习笔记--第六讲--(Aware与InitializingBean接口及@Autowired失效分析)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!