Aware接口
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 {
@Override
public void setBeanName(String name) {
log.info("当前bean "+ this + " 名字叫:" + name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
log.info("当前bean " + this + " 容器是:" + applicationContext);
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("当前bean " + this + "初始化");
}
@Autowired
public void aaa(ApplicationContext applicationContext) {
log.info("当前bean " + this + " 使用!Autowired 容器是:" + applicationContext);
}
@PostConstruct
public void init() {
log.info("当前bean " + this + " 使用@PostConstruct 初始化");
}
}
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 @ImportResource
context.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 {
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) {
log.info("注入 ApplicationContext");
}
@PostConstruct
public 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 {
@Override
public void afterPropertiesSet() throws Exception {
log.info("初始化");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
log.info("注入 ApplicationContext");
}
@Bean // beanFactory后处理器
public BeanFactoryPostProcessor processor1() {
return beanFactory -> {
log.info("执行 processor1");
};
}
}