spring-core-4-32 | BeanDefinition元信息:除了Bean名称和类名,还有哪些Bean元信息值得关注?

本文主要是介绍spring-core-4-32 | BeanDefinition元信息:除了Bean名称和类名,还有哪些Bean元信息值得关注?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

BeanDefinition 元信息

BeanDefinition 元信息一览

在这里插入图片描述

其中一些特别说明:

Name
Bean 的名称或者ID, 也有其它称谓如 Bean的识别符,

Scope
Bean 的作用域(如:singletonprototype 等), 默认是singleton也就是单例模式

Constructor
arguments Bean 构造器参数(用于依赖注入), 正常 POJO类 是有默认构造器的, 这种比较简单.
但如果是引用一些jar中的类, 没有默认构造器, 就需要用这个来帮我们初始化类.

Properties
Bean 属性设置(用于依赖注入), 就是参数注入(Setter注入)

Autowiring mode
Bean 自动绑定模式(如:通过名称byName)

Lazy initialization mode
Bean 延迟初始化模式(延迟和非延迟), 延迟初始化可以有效的减少容器启动的时间

Initialization method
Bean 初始化回调方法名称

Destruction method
Bean 销毁回调方法名称

BeanDefinition 构建

• 通过BeanDefinitionBuilder

• 通过AbstractBeanDefinition 以及派生类

代码示例

/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.  See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package org.geekbang.thinking.in.spring.bean.definition;import org.geekbang.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;/*** 32 | BeanDefinition元信息:除了Bean名称和类名,还有哪些Bean元信息值得关注?* 两种获取 BeanDefinition 实例的方法:* 1. 通过 BeanDefinitionBuilder 构建* 2. 通过 AbstractBeanDefinition 以及派生类** {@link org.springframework.beans.factory.config.BeanDefinition} 构建示例** @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>* @since*/
public class BeanDefinitionCreationDemo {public static void main(String[] args) {// 32.1 通过 BeanDefinitionBuilder 构建// BeanDefinitionBuilder是一个 final 类, 同时包含很多静态方法, 可以帮助我们去初始化BeanDefinitionBuilder// 比如, 添加一个构造参数的值, 可以用 addConstructorArgValue// 如果需要将构造函数中的参数用一个Bean的方式去引用, 可以用 addConstructorArgReference// 如果用 rootBeanDefinition 初始化 BeanDefinitionBuilder, 创建的基本就是祖类BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);// 通过属性设置beanDefinitionBuilder.addPropertyValue("id", 1).addPropertyValue("name", "小马哥");// 获取 BeanDefinition 实例// 里面有常用的 BeanDefinition 元信息BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();// BeanDefinition 并非 Bean 终态,可以自定义修改beanDefinition.setDependsOn("xxxx");// beanDefinitionBuilder.getBeanDefinition() 返回的其实是一个抽象类 AbstractBeanDefinition// 而 BeanDefinition 是个接口,// 另外注意在以往的版本中呢, BeanDefinition 是不能进行set操作的, 后来更新的版本则对此进行了补充// 32.2 通过 AbstractBeanDefinition 以及派生类, 这里可以参考 BeanDefinitionBuilder 的构造器方法来找派生类// GenericBeanDefinition是 AbstractBeanDefinition 的子类GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();// 设置 Bean 类型genericBeanDefinition.setBeanClass(User.class);// 通过 MutablePropertyValues 批量操作属性, genericBeanDefinition 没有 addPropertyValue()MutablePropertyValues propertyValues = new MutablePropertyValues();// MutablePropertyValues有两种操作属性的方式://propertyValues.addPropertyValue("id", 1);//propertyValues.addPropertyValue("name", "小马哥");propertyValues.add("id", 1).add("name", "小马哥");// 通过 set MutablePropertyValues 批量操作属性genericBeanDefinition.setPropertyValues(propertyValues);}
}

这篇关于spring-core-4-32 | BeanDefinition元信息:除了Bean名称和类名,还有哪些Bean元信息值得关注?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/714121

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件