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。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是spring-boot @Component和@Bean的区别详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java