流程图解Spring源码------ bean定义注册源码阅读

流程图详解SpringIOC bean定义的注册


准备样例代码

代码:

在这里插入图片描述

public class InstA {

	public InstA(){
		System.out.println("InstA的构造方法.....");
	}

	@Override
	public String toString() {
		return "InstA{" +
				"instB=" + instB +
				'}';
	}
}
public class InstB {
	public InstB() {
		System.out.println("instB的构造方法");
	}
}
public class InstC {

	public InstC() {
		System.out.println("instC的构造方法");
	}
}
@Component
public class InstD {
	public InstD() {
		System.out.println("InstD的构造方法");
	}
}
public class InstE {
	public InstE() {
		System.out.println("InstE的构造方法");
	}
}
public class InstF {
	public InstF() {
		System.out.println("InstF的构造方法");
	}
}
public class MainClass {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
		Person person= (Person) ctx.getBean("person");
		System.out.println(person.getUserName());
	}
}

配置类

@ComponentScan(basePackages = {"com.tuling.setInject"})
@Import(value = {InstC.class,TulingImportBeanDefinitionRegister.class,TulingImportSelector.class})
@Configuration
public class MainConfig {

	@Bean
	public InstB instB2() {
		return new InstB();
	}

}
@Component
public class Person {

	private String userName;

	private Integer age;

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Person(String userName) {
		this.userName = userName;
		System.out.println("person有参构造器---------userName");
	}

	public Person() {
		System.out.println("person无参构造器");
	}

	public Person(Integer age) {
		this.age = age;
		System.out.println("person有参构造器---------age");

	}

}
@Component
public class TulingBeanDefiantionResigeter implements BeanDefinitionRegistryPostProcessor {
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(InstF.class);
		registry.registerBeanDefinition("instF",rootBeanDefinition);
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

	}
}
@Component
public class TulingBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		GenericBeanDefinition genericBeanDefinition =
				(GenericBeanDefinition) beanFactory.getBeanDefinition("person");
		genericBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("xsls");
		genericBeanDefinition.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
	}
}
public class TulingImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(InstD.class);

		registry.registerBeanDefinition("instD",rootBeanDefinition);
	}
}
public class TulingImportSelector implements ImportSelector {
	@Override
	public String[] selectImports(AnnotationMetadata importingClassMetadata) {
		return new String[]{"com.tuling.setInject.InstE"};
	}
}

流程图解bean定义的注册

图太大了,就直接放我的流程图网站了
流程图解

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值