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

网友投稿 877 2022-11-26

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

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

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

一、@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 bwlRRvIname;

@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);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:树、二叉树、堆
下一篇:题目:Maze(思维+BFS)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~