当前位置: 代码网 > it编程>编程语言>Java > Spring Bean注册的多种方式汇总

Spring Bean注册的多种方式汇总

2026年10月11日 • Java •我要评论
spring 提供了多种注册方式,主要有以下几种1.@component及其衍生注解直接注解业务 class,类被包扫描后自动注册为 bean@componentpublic class employ

spring 提供了多种注册方式,主要有以下几种

1.@component及其衍生注解

直接注解业务 class,类被包扫描后自动注册为 bean

@component
public class employeeservice {}

衍生注解(语义化,底层都是 @component)

// 业务层
@service
// 数据访问层
@repository
// mvc 控制器
@controller
// restful 方式的 @controller
@restcontroller
// 配置类
@configuration

springboot 通过 @springbootapplication 开启包扫描,扫描当前主类所在包及其所有子包。

2.@bean

在@configuration配置类中定义方法,返回对象,注册为 bean

@configuration
public class projconfig {
	  @bean
	  public addressservice addressservice() {
	  	  return new addressservice();
	  }
}

适用场景:

  • 第三方库类(无法修改源码加 @component)
  • 需要显式调用构造 / 工厂方法,自定义对象创建逻辑
  • 复杂对象装配、多参数构建

3.@import

导入配置类 configuration class 或者组件 component

@configuration 
public class shareprojconfig {}

// 方式1:放在启动类
@import(shareprojconfig.class)
@springbootapplication
public class mainapplication {}

// 方式2:放在其他配置类
@import(shareprojconfig.class)
@configuration
public class projconfig {}

4.@importresource

兼容老项目,加载 xml 配置文件,从 xml 中注册 bean

<bean id="addressservice" class="pr.iceworld.fernando.addressservice">
...
<bean id=...>
@configuration
@importresource("classpath:beans.xml")
public class projconfig{
}

5.importbeandefinitionregistrar(动态注册 beandefinition)

注意:beandefinitionregistry 是注册器对象,不是接口名!
实现importbeandefinitionregistrar,动态注册 beandefinition,框架级动态注册

public class customconfig implements importbeandefinitionregistrar {
    @override
    public void registerbeandefinitions(annotationmetadata metadata, beandefinitionregistry registry) {
        beandefinition bd = beandefinitionbuilder
                .genericbeandefinition(addressservice.class)
                .getbeandefinition();
        registry.registerbeandefinition("addressservice", bd);
    }
}

特点:在 bean 实例化前注册 beandefinition,多用于框架扩展。

6.beandefinitionregistrypostprocessor

bean 工厂后置处理器,在所有 beandefinition 加载完成、bean 实例化之前新增 / 修改 bean 定义

@component
public class custombeanregistrar implements beandefinitionregistrypostprocessor {
	@override
	public void postprocessbeandefinitionregistry(beandefinitionregistry registry) {
		beandefinition bd = beandefinitionbuilder
				.rootbeandefinition(addressservice.class)
				.getbeandefinition();
		registry.registerbeandefinition("addressservice", bd);
	}
	@override
	public void postprocessbeanfactory(configurablelistablebeanfactory beanfactory) {}
}

适用:底层架构、中间件框架开发。

7.registersingleton直接注册单例对象

向 beanfactory 注册已经 new 好的实例对象

重点:该对象不会执行 spring 完整 bean 生命周期(不会执行 initializingbean、aware 回调等)

@configuration
public class runtimeprojconfig {
	@bean
	public beanfactorypostprocessor registrar() {
		return beanfactory -> {
			beanfactory.registersingleton("addressservice", new addressservice());
		};
	}
}

8.@configurationpropertiesscan

扫描带有@configurationproperties的配置绑定类,读取配置文件映射到 java 对象。

作用:注册配置属性类 bean,绑定 properties/yaml 配置,不是通用业务 bean 注册

application.properties

proj.name=xxx
@configurationproperties(prefix = "proj")

public class projproperties {
	private string name;
	// getter / setter
}

@configurationpropertiesscan
@springbootapplication
public class mainapplication {}

9.@enablexxx系列注解(间接注册 bean)

底层依靠@import导入内部配置类,由配置类注册对应的 bean,属于间接方式

@enablescheduling
@enablecaching
@enableasync
@enabletransactionmanagement
@enableasync
@springbootapplication
public class mainapplication {}

总结

场景推荐方式
自己项目内业务类,可直接加注解@component / @service / @repository / @controller
第三方类、需要自定义构造创建对象@bean
导入外部配置类、组件@import
老项目兼容xml bean定义@importresource
配置文件绑定属性类@configurationpropertiesscan
框架动态注册beandefinitionimportbeandefinitionregistrar / beandefinitionregistrypostprocessor
直接注入已实例化对象beanfactory.registersingleton
  1. @import 支持 3 种类型:configuration类 / importselector / importbeandefinitionregistrar
  2. beandefinition 只是bean 的元数据描述,此时对象还没实例化;registersingleton是直接注册成品实例。
  3. 优先级:beandefinitionregistrypostprocessor 执行时机早于普通 beanfactorypostprocessor。

以上就是spring bean注册的多种方式汇总的详细内容,更多关于spring bean注册多种方式的资料请关注代码网其它相关文章!

赞 (0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com