Spring Boot中@Component与@Bean有何本质区别?
- 内容介绍
- 文章标签
- 相关推荐
本文共计259个文字,预计阅读时间需要2分钟。
1. 使用 @Component 注解在类上,定义了一个公共类 Student,其中包含私有属性 name 和对应的 getter 和 setter 方法。
2.使用 @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。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计259个文字,预计阅读时间需要2分钟。
1. 使用 @Component 注解在类上,定义了一个公共类 Student,其中包含私有属性 name 和对应的 getter 和 setter 方法。
2.使用 @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。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

