spring-boot中@Component与@Bean的区别是什么
更新:HHH   时间:2023-1-7


这期内容当中小编将会给大家带来有关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的区别是什么了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注天达云行业资讯频道。

返回编程语言教程...