spring-boot中@Component和@Bean的区别是什么

2023-05-19,,

这篇文章主要为大家展示了“spring-boot中@Component和@Bean的区别是什么”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“spring-boot中@Component和@Bean的区别是什么”这篇文章吧。

1、@Component 是用在类上的

@Component 
public class Student { private String name = "lkm";
public String getName() { return name;  } 
public void setName(String name) {   this.name = name;  } }

2、@Bean 需要在配置类中使用,即类上需要加上@Configuration注解

@Configuration
public class WebSocketConfig {  
@Bean  
public Student student(){    return new Student();  }}

如果你想要将第三方库中的组件装配到你的应用中,在这种情况下,是没有办法在它的类上添加@Component注解的,因此就不能使用自动化装配的方案了,但是我们可以使用@Bean。

以上是“spring-boot中@Component和@Bean的区别是什么”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注本站行业资讯频道!

《spring-boot中@Component和@Bean的区别是什么.doc》

下载本文的Word格式文档,以方便收藏与打印。