原文链接:https://blog.csdn.net/weixin_42493179/article/details/86584341
1、@Component注解表明一个类会作为组件类,并告知Spring要为这个类创建bean。
2、@Bean注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。通常方法体中包含了最终产生bean实例的逻辑。
两者的目的是一样的,都是注册bean到Spring容器中。
区别:
@Component(@Controller、@Service、@Repository)通常是通过类路径扫描来自动侦测以及自动装配到Spring容器中。
而@Bean注解通常是我们在标有该注解的方法中定义产生这个bean的逻辑。
@Component 作用于类,@Bean作用于方法。
总结:
@Component和@Bean都是用来注册Bean并装配到Spring容器中,但是Bean比Component的自定义性更强。可以实现一些Component实现不了的自定义加载类。
直接使用一段代码就可以明显的看出他们之间的区别
Student类
public class Student {
private String name = "lkm";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
使用component注册
@Component
public class WebSocketConfig {
public Student student(){
return new Student();
}
}
这里只会注册WebSocketConfig并不会注册Student类
就是Spring容器中只有WebSocketConfig不存在Student
使用Bean注解
@Configuration
public class WebSocketConfig {
@Bean
public Student student(){
return new Student();
}
}
这里既会在Spring容器中注册WebSocketConfig也会注册Student
@Component作用在类上
@Bean作用在方法上,一般这个方法都会返回一个对象。