spring-boot @Component和@Bean的区别介绍

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring-boot @Component和@Bean的区别介绍

  2021-04-03 我要评论

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。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们