Spring手动构建BeanDefinition的几种方法

Spring手动构建BeanDefinition的几种方法

  • GenericBeanDefinition
  • RootBeanDefinition
  • ChildBeanDefinition
  • BeanDefinitionBuilder

GenericBeanDefinition

@Data
public class Student {
    String name;
    int age;
}
public void test() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	GenericBeanDefinition gBean = new GenericBeanDefinition();
	//设置bean
	gBean .setBeanClass(Student.class);
	//设置属性
	MutablePropertyValues propertys=new MutablePropertyValues();
	propertys.add("name","luxifa").add("age",25);
	gBean .setPropertyValues(mp); 
	// 注册bean
    context.registerBeanDefinition("bean1", gBean );
}

GenericBeanDefinition也可以继承

public void test() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	GenericBeanDefinition definition1 = new GenericBeanDefinition();
	//设置bean
	definition1.setBeanClass(Student.class);
	//设置属性
	MutablePropertyValues propertys=new MutablePropertyValues();
	propertys.add("name","luxifa").add("age",25);
	definition1.setPropertyValues(mp); 
	// 注册bean
    context.registerBeanDefinition("gBean1", definition1);


    GenericBeanDefinition definition2 = new GenericBeanDefinition();
    definition2.setParentName("gBean1");
    // definition2 的属性继承了 definition1
    context.registerBeanDefinition("gBean2", definition2);
    
	context.refresh();

    Student student1 = (Student) context.getBean("gBean1");
    Student bean2 = (Student) context.getBean("gBean2");
}


RootBeanDefinition

RootBeanDefinition不能有父节点

public void test() {
    // RootBeanDefinition
    RootBeanDefinition definition = new RootBeanDefinition();
    definition.setBeanClass(Student.class);
    definition.getPropertyValues().add("name", "luxifa");
    definition.getPropertyValues().add("age", 25);
    context.registerBeanDefinition("bean1", definition);

    // child
    ChildBeanDefinition definition2 = new ChildBeanDefinition("bean1");
    context.registerBeanDefinition("bean2", definition2);

    // 刷新
    context.refresh();

    Bean bean1 = (Bean) context.getBean("bean1");
    Bean bean2 = (Bean) context.getBean("bean2");
    System.out.println(bean1==bean2);
}

ChildBeanDefinition

ChildBeanDefinition必须有父节点

public void test() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    // RootBeanDefinition
    RootBeanDefinition definition = new RootBeanDefinition();
    definition.setBeanClass(Student.class);
    definition.getPropertyValues().add("name", "luxifa");
    definition.getPropertyValues().add("age", 25);
    context.registerBeanDefinition("rBean1", definition);

    // ChildBeanDefinition
    ChildBeanDefinition definition2 = new ChildBeanDefinition("rBean1");
    context.registerBeanDefinition("rBean2", definition2);
}

BeanDefinitionBuilder

BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Student.class);
//懒加载,调用get("beanname")方法时才实例化
builder.setLazyInit(true);
// builder.getBeanDefinition() 其实是一个 GenericBeanDefinition
context.registerBeanDefinition("hBean", builder.getBeanDefinition());

参考博客:https://juejin.cn/post/6844904118595616782

你可能感兴趣的:(spring,java,后端)