栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring内Bean的BeanDefiniton类型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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;



//@Service
public class AutoService {

}



	
	

@Component注解

package com.chuliuhuan.domain;

import org.springframework.stereotype.Component;


@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;


@Configuration
@ImportResource("services.xml")
@ComponentScan("com.chuliuhuan.domain")
public class AppConfig {
	@Bean
	public TransferService transferService() {
		return new TransferServiceImpl();
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/999179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号