把Bean交给spring容器的方式:
- xml 声明bean <bean id="", class="">
- @ConponentScan + @Sevice/@Controller/@Repository/@Componet
- @Import(XXX.class)
- ImportSelector 接口 -> 返回类名数组
- ImportBeanDefinitionRegistrar 接口 -> registerBeanDefinitions
- @Bean 注解
- FactoryBean 接口 -> getObject()
- SingletonBeanRegistry.registerSingleton();
- register(Class<?>... annotatedClasses)
- registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
方式1,方式2常规使用方法,如果这都不会,那就不要看了
方式3:@Import(XXX.class)
其实方式2和方式3实现是一样的,都是通过ConfigurationClassPostProcessor。
ConfigurationClassPostProcessor是一个BeanFactory的后置处理器,它的主要功能是参与BeanFactory的建造。在这个类中,会解析加了@Configuration的配置类,还会解析@ComponentScan、@ComponentScans注解扫描的包,以及解析@Import等注解。
public class Blue {
}
public class Yellow {
}
public class Color {
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Color [car=" + car + "]";
}
}
@Configuration
@Import({Yellow.class, Blue.class, Color.class})
public class MainConfig05 {
}
public class mainTest05 {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig05.class);
@Test
public void test() {
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach(System.out::println);
}
}
测试结果
...
org.springframework.context.event.internalEventListenerFactory
mainConfig05
com.evan.bean.Yellow
com.evan.bean.Blue
com.evan.bean.Color
方式4: ImportSelector 接口 -> 返回类名数组
ImportSelector,输入选择器。该接口就是用来根据给定的条件,选择导入哪些配置类。
// 自定义逻辑返回需要导入的组件
public class MyImportSelector implements ImportSelector {
/**
* @param annotationMetadata 当前标注 @import 注解的类的所有注解信息
* @return 该方法的返回值是一个String[]数组。向数组中添加类的全类名,这样就能将该类注册到Spring容器中了
*/
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
Set<String> annotationTypes = annotationMetadata.getAnnotationTypes();
System.out.println("=============");
for (String annotation : annotationTypes){
System.out.println(annotation);
}
System.out.println("=============");
for (String str : annotationTypes) {
System.out.println(str);
}
System.out.println("=============");
return new String[]{"com.evan.bean.Blue","com.evan.bean.Yellow"};
}
}
@Configuration
@Import({MyImportSelector.class})
public class MainConfig07 {
}
public class mainTest07 {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig07.class);
@Test
public void test() {
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach(System.out::println);
}
}
打印结果
=============
org.springframework.context.annotation.Configuration
org.springframework.context.annotation.Import
=============
org.springframework.context.annotation.Configuration
org.springframework.context.annotation.Import
=============
....
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig07
com.evan.bean.Blue
com.evan.bean.Yellow
或者使用自定义注解
package com.evan.annotation;
import com.evan.condition.MyImportSelector;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
@Import(MyImportSelector.class)
public @interface EnableMyImportSelector {
String name ();
}
在配置泪上加上自定义注解。其他不变
@EnableMyImportSelector(name = "test")
public class MainConfig07 {
}
方式5:ImportBeanDefinitionRegistrar 接口 -> registerBeanDefinitions
public class RainBow {
}
public class Blue {
private String name;
public void setName(String name) {
this.name = name;
}
}
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
/**
* 把需要添加到容器中的bean; 通过 beanDefinitionRegistry.registerBeanDefinition() 手工注册到容器中
*
* @param annotationMetadata 当前类的注解信息及其他信息
* @param beanDefinitionRegistry beanDefinition 注册类
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
// 指定被注册bean 的id名
System.out.println("通过registerBeanDefinition()将RainBow 注入容器中");
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(RainBow.class);
beanDefinitionRegistry.registerBeanDefinition("rainBow", rootBeanDefinition);
BeanDefinitionBuilder blue1 = BeanDefinitionBuilder.rootBeanDefinition(Blue.class);
blue1.addPropertyValue("name", "蓝色");
beanDefinitionRegistry.registerBeanDefinition("蓝色", blue1.getBeanDefinition());
}
}
@Import({MyImportBeanDefinitionRegistrar.class})
public class MainConfig08 {
}
public class mainTest08 {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig08.class);
@Test
public void test() {
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach(System.out::println);
}
}
输出结果
通过registerBeanDefinition()将RainBow 注入容器中
16:00:15.248 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
16:00:15.282 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
16:00:15.284 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
16:00:15.291 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@351d0846]
16:00:15.292 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@64d2d351]
16:00:15.293 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
16:00:15.299 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
16:00:15.301 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mainConfig08'
16:00:15.302 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rainBow'
16:00:15.303 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean '蓝色'
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig08
rainBow
蓝色
方式6:@Bean 注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
//使用@Value赋值;
//1、基本数值
//2、可以写SpEL; #{}
//3、可以写${};取出配置文件【properties】中的值(在运行环境变量里面的值)
@Value("张三")
private String name;
@Value("#{20-2}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
}
@Configuration
public class MainConfig01 {
@Bean
public Person person() {
return new Person();
}
}
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig01.class);
@Test
public void test01() {
printBeans(applicationContext);
// 根据类型获取
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
// 根据类型获取
String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
for (String beanName : beanNamesForType) {
System.out.println(beanName);
}
System.out.println("====================");
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanDefinitionNames) {
System.out.println(beanName);
}
}
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig01
person
Person(name=张三, age=18, nickName=${person.nickName})
person
====================
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig01
person
方式7:FactoryBean 接口 -> getObject()
Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean。
Spring FactoryBean是创建复杂的bean,一般的bean直接用注解即可, 如果一个bean的创建过程中涉及到很多其他的bean和复杂的逻辑, 用注解配置比较困难,这时可以考虑用FactoryBean.
这两种Bean都被容器管理,但FactoryBean跟普通Bean不同,其返回的对象不是指定类的一个实例, 其返回的是该FactoryBean的getObject方法所返回的对象。
FactoryBean的源码如下:
public interface FactoryBean<T> {
//返回的对象实例
T getObject() throws Exception;
//Bean的类型
Class<?> getObjectType();
//true是单例,false是非单例 在Spring5.0中此方法利用了JDK1.8的新特性变成了default方法,返回true
boolean isSingleton();
在该接口中定义了三个方法:
-
T getObejct(): 返回由FactoryBean创建的bean实例,如果isSingleton()返回true,则该实例会放到Spring容器中实例缓存池中。
-
boolean isSingleton():返回有FactoryBean创建的bean的实例的作用域是singleton还是prototype,这里默认返回的是true,也就是默认是singleton bean。
-
Class getObjectType(): 返回FactoryBean创建的 bean的类型。
@Data
public class Colors {
private int red;
private int green;
private int blue;
}
package com.evan.factory;
import com.evan.bean.Colors;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;
// 创建一个spring定义的FactoryBean
@Component
public class ColorFactoryBean implements FactoryBean<Colors> {
/**
* 返回一个 Color 对象,这个对象会添加到容器中
*
* @return
* @throws Exception
*/
@Override
public Colors getObject() throws Exception {
System.out.println("ColorFactoryBean 的 getObject()方法被调用");
Colors color = new Colors();
color.setRed(100);
color.setGreen(100);
color.setBlue(20);
return color;
}
/**
* 把传入的Class进行注册,Class既可以有@Configuration注解,也可以没有@Configuration注解
* 怎么注册? 委托给了 org.springframework.context.annotation.AnnotatedBeanDefinitionReader.register 方法进行注册
* 传入Class 生成 BeanDefinition , 然后通过 注册到 BeanDefinitionRegistry
*/
@Override
public Class<?> getObjectType() {
return Colors.class;
}
/**
* 是否单例
* true : 这个bean 是单实例,在容器中保存一份
* false : 多实例 每次获取都会创建一个新的bean
*
* @return
*/
@Override
public boolean isSingleton() {
return true;
}
}
@Configuration
@ComponentScan("com.evan.factory")
public class MainConfig10 {
}
public class mainTest10 {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig10.class);
@Test
public void test() {
System.out.println("容器启动完成");
Object colorFactoryBean = applicationContext.getBean("colorFactoryBean");
System.out.println(colorFactoryBean);
Colors bean = applicationContext.getBean(Colors.class);
System.out.println(bean);
System.out.println("---------------");
//要获取工厂Bean 本身,需要给id前面加一个&
Object bean4 = applicationContext.getBean("&colorFactoryBean");
System.out.println("要获取工厂Bean 本身,需要给id前面加一个&, >>>>:" + bean4.getClass());
System.out.println("---------------");
}
}
....
容器启动完成
ColorFactoryBean 的 getObject()方法被调用
Colors(red=100, green=100, blue=20)
Colors(red=100, green=100, blue=20)
---------------
要获取工厂Bean 本身,需要给id前面加一个&, >>>>:class com.evan.factory.ColorFactoryBean
---------------
方式8:SingletonBeanRegistry.registerSingleton();
SingletonBeanRegistry
public interface SingletonBeanRegistry {
//检查此注册表是否包含具有给定名称的单例实例。
boolean containsSingleton(String beanName)
//返回在给定名称下注册的(原始)单例对象。
Object getSingleton(String beanName)
//返回在此注册表中注册的单例bean的数量。
int getSingletonCount()
//返回此注册表使用的单例互斥锁(对于外部协作者)。
Object getSingletonMutex()
//返回在此注册表中注册的单例bean的名称。
String[] getSingletonNames()
//在给定的bean名称下,在bean注册表中将给定的现有对象注册为singleton。
void registerSingleton(String beanName, Object singletonObject)
}
SingletonBeanRegistry被誉为Spring的单例Bean定注册中心。
demo
@ToString
public class Car {
private String name;
public Car() {
System.out.println("car constructor ....");
this.name = "BMW";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Boss {
@Value("evan")
private String name;
private Car car;
//构造器要用的组件,都是从容器中获取
public Boss(Car car) {
this.car = car;
System.out.println("Boss...有参构造器");
}
public Car getCar() {
return car;
}
public String getCarName() {
System.out.println(car.getName());
return car.getName();
}
//@Autowired
//标注在方法,Spring容器创建当前对象,就会调用方法,完成赋值;
//方法使用的参数,自定义类型的值从ioc容器中获取
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Boss [car=" + car + "]";
}
}
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
@Test
public void test02() {
Car car = new Car();
Boss boss = new Boss(car);
applicationContext.getBeanFactory().registerSingleton("boss", boss);
// 需要执行refresh()方法,否则会报错
// java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext has not been refreshed yet
applicationContext.refresh();
System.out.println("refresh 后");
Boss bean = applicationContext.getBean(Boss.class);
bean.getCarName();
System.out.println(boss == bean); //true
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach(System.out::println);
}
car constructor ....
Boss...有参构造器
22:19:13.513 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7f63425a: startup date [Wed Nov 23 22:19:13 CST 2022]; root of context hierarchy
22:19:13.514 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@7f63425a: org.springframework.beans.factory.support.DefaultListableBeanFactory@578486a3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory]; root of factory hierarchy
22:19:13.525 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
22:19:13.553 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
22:19:13.585 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
22:19:13.587 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
22:19:13.595 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@4e41089d]
22:19:13.596 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@76329302]
22:19:13.597 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
22:19:13.603 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
22:19:13.619 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@69b794e2]
refresh 后
BMW
true
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
方式9:register(Class<?>... annotatedClasses)
public class Yellow {
}
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
@Test
public void test01() {
applicationContext.register(Yellow.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach(System.out::println);
}
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
yellow
方式10:registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
@ToString
public class Car {
private String name;
public Car() {
System.out.println("car constructor ....");
this.name = "BMW";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Test
public void test03() {
MutablePropertyValues values=new MutablePropertyValues();
values.add("name","特斯拉");
BeanDefinition definition=new RootBeanDefinition(Car.class,null,values);
applicationContext.registerBeanDefinition("car",definition);
String[] beanDefinitionNames1 = applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames1).forEach(System.out::println);
// 需要执行refresh()方法,否则会报错
// java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext has not been refreshed yet
applicationContext.refresh();
System.out.println("refresh 后");
Car bean = applicationContext.getBean(Car.class);
System.out.println("调用bean.getName():"+bean.getName());
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach(System.out::println);
}
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
car
22:07:36.826 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7f63425a: startup date [Wed Nov 23 22:07:36 CST 2022]; root of context hierarchy
22:07:36.826 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@7f63425a: org.springframework.beans.factory.support.DefaultListableBeanFactory@49fc609f: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,car]; root of factory hierarchy
22:07:36.836 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
22:07:36.864 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
22:07:36.898 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
22:07:36.901 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
22:07:36.910 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@7fad8c79]
22:07:36.911 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@31206beb]
22:07:36.912 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
22:07:36.918 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
22:07:36.920 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'car'
car constructor ....
22:07:36.946 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@4ae3c1cd]
refresh 后
调用bean.getName():特斯拉
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
car