spring内Bean的BeanDefiniton类型

spring内Bean的BeanDefiniton类型

存在的类型:

生成bean的方式BeanDefinition类型
@ConfigurationAnnotatedGenericBeanDefinition
@BeanConfigurationClassBeanDefinition
xml < bean/>标签GenericBeanDefinition
@ComponentScannedGenericBeanDefinition

其实注解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();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值