广告
返回顶部
首页 > 资讯 > 后端开发 > Python >@Bean注解和@Configuration、@Component注解组合使用的区别
  • 562
分享到

@Bean注解和@Configuration、@Component注解组合使用的区别

2024-04-02 19:04:59 562人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录一、@Bean的“full”模式和“lite”模式二、两种模式的差异1、“full”模式下@Bean方法互相调用2、“lite”模式下@Bean方法互相调用三、总结 一、@Bea

一、@Bean的“full”模式和“lite”模式

在一般常见情况下,@Bean注解在@Configuration类中声明,称之为“full”模式;当@Bean注解和@Component注解组合使用时,称之为“lite”模式。

这两种组合使用的情况,初次看文档时没看明白,多看了几次又跑了测试代码,才大致理解了区别。

二、两种模式的差异

如果只是把@Bean注解用在方法上,并且各个@Bean注解的方法之间没有调用,上述两种模式达到的效果基本相同,都可以把@Bean注解方法返回的对象作为bean注册到容器中。

如果各个@Bean注解的方法之间有相互调用,那么两种模式就会有很大的区别-与full模式下的@Configuration不同,lite模式下 @Bean方法互相调用无法声明Bean之间的依赖关系。

这点在@Bean注解源码注释上也有说明。

1、“full”模式下@Bean方法互相调用


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;  
@Configuration
public class Config {
 
    @Bean()
    public C c(){
        return new C();
    }
 
    @Bean
    public B b(){
        return new B(c());
    } 
}

类B:


public class B { 
    public C c; 
    public B(C a){this.c=a;} 
    public void shutdown(){
        System.out.println("b close...");
    }
}

类C:


public class C { 
    private String name; 
    @PostConstruct
    public void init(){
        this.name = "I am a bean";
    }
 
    @Override
    public String toString(){
        return "c say:" + name;
    }
}

如上述所示代码,我们有两个类B和C,在@Configuration类中,使用两个@Bean方法分别定义bean b 和bean c,但是需要注意的是new B(c()) 所在的@Bean方法调用了另一个@Bean方法。

测试主程序类:


import com.dxc.opentalk.springtest.config.B;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy; 
 
@EnableAspectJAutoProxy
@ComponentScan("com.dxc.opentalk.springtest")
public class BootStrap { 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext
                = new AnnotationConfigApplicationContext(BootStrap.class);
        B b = (B)applicationContext.getBean("b");
        System.out.println(b.c);
        System.out.println(applicationContext.getBean("c"));
        System.out.println(applicationContext.getBean("c").equals(b.c));
    }
}

程序输出结果:

c say:I am a bean
c say:I am a bean
true

可以看出bean c 被注入到了bean b 中,bean b中的成员 c 确实是Spring容器中的bean。(其实,debug程序跟踪Spring中的单例池更清晰,这里采用输出结果说明)

2、“lite”模式下@Bean方法互相调用

还是上面的代码,我们只把Config类上的注解更换成@Component,其余代码保持不变:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;  
@Component
public class Config {
 
    @Bean()
    public C c(){
        return new C();
    }
 
    @Bean
    public B b(){
        return new B(c());
    }
}

再看一下测试主程序类输出结果:

c say:null
c say:I am a bean
false

可以看到bean b中的c只是一个普通的c对象,并不是Spring容器中的bean(b中的c没有执行bean的初始化回调方法也和单例池中的c bean不相等)。所以这种模式下,@Bean方法互相调用不能完成bean之间相互依赖关系的注入。

三、总结

综上所述,在使用@Bean注解时需要注意两种模式的区别,一般情况下@Bean都是和@Configuration注解搭配使用的。

但是@Configuration注解的类会被Spring使用CGLIB子类化,所以@Configuration注解的类不能用 final 修饰,而@Component注解没有此限制。

如果使用@Bean注解和@Component注解组合需要完成bean之间相互依赖注入的话,官方推荐采用构造方法或者方法级别的依赖注入,如下:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
 
@Component
public final class Config {
 
    @Bean()
    public C c(){
        return new C();
    } 
    @Bean
    public B b(C c){//构造方法注入
        return new B(c);
    } 
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: @Bean注解和@Configuration、@Component注解组合使用的区别

本文链接: https://www.lsjlt.com/news/156160.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作