流程图详解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定义的注册
图太大了,就直接放我的流程图网站了
流程图解