【spring】BeanDefinition接口学习

BeanDefinition接口介绍

BeanDefinition 接口是 Spring 框架中用于描述 Bean 定义的核心接口。它定义了 Bean 的各种元数据信息,这些信息用于在 Spring 容器中创建和管理 Bean 实例。BeanDefinition 接口是 Spring 容器在运行时用来表示 Bean 定义的抽象,它包含了 Bean 的类名、作用域、构造函数参数、属性值、初始化方法、销毁方法等信息。

使用场景

BeanDefinition 主要用于 Spring 容器的内部实现,开发者通常不需要直接操作 BeanDefinition 接口。Spring 容器在解析配置文件(如 XML、Java 配置类)或注解时,会自动生成相应的 BeanDefinition 对象,并使用这些对象来创建和管理 Bean 实例。

简化代码(源代码)

package org.springframework.beans.factory.config;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.core.AttributeAccessor;
import org.springframework.lang.Nullable;

/**
 * A BeanDefinition describes a bean instance, which has property values,
 * constructor argument values, and further information supplied by
 * concrete implementations.
 *
 * <p>This is just a minimal interface: The main intention is to allow a
 * {@link BeanFactoryPostProcessor} such as {@link PropertyPlaceholderConfigurer}
 * to introspect and modify property values and other bean metadata.
 *
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @since 19.03.2004
 * @see ConfigurableListableBeanFactory#getBeanDefinition
 * @see org.springframework.beans.factory.support.RootBeanDefinition
 * @see org.springframework.beans.factory.support.ChildBeanDefinition
 */
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

	String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

	String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;

	int ROLE_APPLICATION = 0;

	int ROLE_SUPPORT = 1;

	int ROLE_INFRASTRUCTURE = 2;

	void setParentName(@Nullable String parentName);

	@Nullable
	String getParentName();

	void setBeanClassName(@Nullable String beanClassName);

	@Nullable
	String getBeanClassName();

	void setScope(@Nullable String scope);

	@Nullable
	String getScope();

	void setLazyInit(boolean lazyInit);

	boolean isLazyInit();

	void setDependsOn(@Nullable String... dependsOn);

	@Nullable
	String[] getDependsOn();

	void setAutowireCandidate(boolean autowireCandidate);

	boolean isAutowireCandidate();

	void setPrimary(boolean primary);

	boolean isPrimary();

	void setFactoryBeanName(@Nullable String factoryBeanName);

	@Nullable
	String getFactoryBeanName();

	void setFactoryMethodName(@Nullable String factoryMethodName);

	@Nullable
	String getFactoryMethodName();

	ConstructorArgumentValues getConstructorArgumentValues();

	default boolean hasConstructorArgumentValues() {
		return !getConstructorArgumentValues().isEmpty();
	}

	MutablePropertyValues getPropertyValues();

	default boolean hasPropertyValues() {
		return !getPropertyValues().isEmpty();
	}

	void setInitMethodName(@Nullable String initMethodName);

	@Nullable
	String getInitMethodName();

	void setDestroyMethodName(@Nullable String destroyMethodName);

	@Nullable
	String getDestroyMethodName();

	void setRole(int role);

	int getRole();

	void setDescription(@Nullable String description);

	String getDescription();

	boolean isSingleton();

	boolean isPrototype();

	boolean isAbstract();

	@Nullable
	String getResourceDescription();

	@Nullable
	BeanDefinition getOriginatingBeanDefinition();

}

实现类

BeanDefinition 接口有多个实现类,常见的实现类包括:

  • RootBeanDefinition: 通常用于定义单个 Bean 的定义。

  • ChildBeanDefinition: 用于定义子 Bean 的定义,继承父 Bean 的配置。

  • GenericBeanDefinition: 通用的 Bean 定义,可以作为父 Bean 或子 Bean 使用。

  • AnnotatedBeanDefinition: 用于表示通过注解定义的 Bean。

主要属性和方法

以下是 BeanDefinition 接口中一些重要的属性和方法:

  • Class Name (getBeanClassName()setBeanClassName()):

    • 获取或设置 Bean 的类名。

  • Scope (getScope()setScope()):

    • 获取或设置 Bean 的作用域(例如:singletonprototype)。

  • Lazy Initialization (isLazyInit()setLazyInit()):

    • 获取或设置 Bean 是否为延迟初始化。

  • Primary (isPrimary()setPrimary()):

    • 获取或设置 Bean 是否为主要候选者(在多个 Bean 满足依赖时优先选择)。

  • Factory Method (getFactoryMethodName()setFactoryMethodName()):

    • 获取或设置用于创建 Bean 的工厂方法名。

  • Constructor Arguments (getConstructorArgumentValues()):

    • 获取构造函数参数值。

  • Property Values (getPropertyValues()):

    • 获取 Bean 的属性值。

  • Init Method (getInitMethodName()setInitMethodName()):

    • 获取或设置 Bean 的初始化方法名。

  • Destroy Method (getDestroyMethodName()setDestroyMethodName()):

    • 获取或设置 Bean 的销毁方法名。

  • Depends On (getDependsOn()setDependsOn()):

    • 获取或设置 Bean 依赖的其他 Bean。

  • Role (getRole()):

    • 获取 Bean 的角色(例如:ROLE_APPLICATIONROLE_SUPPORTROLE_INFRASTRUCTURE)。

  • Description (getDescription()setDescription()):

    • 获取或设置 Bean 的描述信息。

示例代码

使用 BeanDefinitionBuilder 创建了一个 BeanDefinition,并将其注册到 DefaultListableBeanFactory 中,然后通过 BeanFactory 获取 Bean 实例。

class DemoBean {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

public class BeanDefinitionDemo {
    public static void main(String[] args) {
        // 创建 BeanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        // 使用 BeanDefinitionBuilder 创建 BeanDefinition
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DemoBean.class);
        builder.addPropertyValue("name", "chenxuyuanshitang");

        // 注册 BeanDefinition
        beanFactory.registerBeanDefinition("demoBean", builder.getBeanDefinition());

        // 获取 Bean 实例
        MyBean myBean = beanFactory.getBean(DemoBean.class);
        // 输出: chenxuyuanshitang
        System.out.println(myBean.getName()); 
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值