Spring中构建BeanDefinition的两种方法

  • BeanDefinitionBuilder
  • GenericBeanDefinition

RumenzA.class

package com.rumenz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

public class RumenzA {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public  RumenzA() {
        System.out.println("RumenzA 无参构造方法");
    }
}

BeanDefinitionBuilder

 //1.通过BeanDefinitionBuilder
 BeanDefinitionBuilder bdb=BeanDefinitionBuilder.genericBeanDefinition(RumenzA.class);
 //设置属性
 bdb.addPropertyValue("id",1).addPropertyValue("name","入门小站");
 //获取BeanDefinition
 BeanDefinition bd=bdb.getBeanDefinition();
 //后期的BeanDefinition还是可以修改的
 System.out.println(bd);
 

GenericBeanDefinition

 //2.通过GenericBeanDefinition
 GenericBeanDefinition gb= new GenericBeanDefinition();
 //设置bean
 gb.setBeanClass(RumenzA.class);
 //设置属性
 MutablePropertyValues mp=new MutablePropertyValues();
 mp.add("id",1).add("name","入门小站");
 gb.setPropertyValues(mp);
 System.out.println(gb);

WechatIMG8.png

原文: [https://rumenz.com/rumenbiji/...]
(https://rumenz.com/rumenbiji/...

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