spring内Bean的BeanDefiniton类型
存在的类型:
生成bean的方式 | BeanDefinition类型 |
---|---|
@Configuration | AnnotatedGenericBeanDefinition |
@Bean | ConfigurationClassBeanDefinition |
xml < bean/>标签 | GenericBeanDefinition |
@Component | ScannedGenericBeanDefinition |
其实注解AppConfig配置类可以从源码解析
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(AppConfig.class);
第一行代码 生成AnnotatedBeanDefinitionReader reader
、ClassPathBeanDefinitionScanner scanner
第二行代码:new 一个BeanDefiniton 对象并根据条件给对象设置属性
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
测试验证代码
测试结果:
appConfig配置类对应的BeanDefiniton:
org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition
@Bean 对应的BeanDefiniton:
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader$ConfigurationClassBeanDefinition
xml bean标签:
org.springframework.beans.factory.support.GenericBeanDefinition
@Component 注解对应的beanDefiniton:
org.springframework.context.annotation.ScannedGenericBeanDefinition
测试Test类
package com.chuliuhuan.java.util;
import com.chuliuhuan.config.AppConfig;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author chuliuhuan
* @date 2021-06-22 21:33
*/
public class CustomerCollectionTest {
@Test
public void initContext() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
BeanDefinition appConfig = context.getBeanDefinition("appConfig");
System.out.println("appConfig配置类对应的BeanDefiniton:\n"+appConfig.getClass().getName());
System.out.println("@Bean 对应的BeanDefiniton:\n"+context.getBeanDefinition("transferService").getClass().getName());
System.out.println("xml bean标签:\n"+context.getBeanDefinition("autoService").getClass().getName());
System.out.println("@Component 注解对应的beanDefiniton:\n"+context.getBeanDefinition("indexService").getClass().getName());
}
}
XML bean标签
package com.chuliuhuan.domain;
/**
* @author chuliuhuan
* @date 2022-06-24 22:29
*/
//@Service
public class AutoService {
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="autoService" class="com.chuliuhuan.domain.AutoService">
</bean>
</beans>
@Component注解
package com.chuliuhuan.domain;
import org.springframework.stereotype.Component;
/**
* @author chuliuhuan
* @date 2022-06-24 22:28
*/
@Component
public class IndexService {
public IndexService() {
}
}
AppConfig配置类 和@Bean 注解生产的Bean
package com.chuliuhuan.config;
import com.chuliuhuan.domain.TransferService;
import com.chuliuhuan.domain.TransferServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* @author chuliuhuan
* @date 2022-06-24 22:36
*/
@Configuration
@ImportResource("services.xml")
@ComponentScan("com.chuliuhuan.domain")
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}