Java注解中@Component和@Bean的區(qū)別
個人回答:
1.作用對象不同:@Component 注解作用于類,而 @Bean 注解作用于方法、
2.@Component 通常是通過路徑掃描來自動偵測以及自動裝配到 Spring 容器中(我們可以使用 @ComponentScan 注解定義要掃描的路徑從中找出標識了需要裝配的類自動裝配到 Spring 的 bean 容器中)。@Bean 注解通常是我們在標有該注解的方法中定義產(chǎn)生這個 bean,
@Bean 告訴了 Spring 這是某個類的實例,當我們需要用它的時候還給我。
3.@Bean 注解比 @Component 注解的自定義性更強,而且很多地方我們只能通過 @Bean 注解來注冊 bean。比如當我們引用第三方庫中的類需要裝配到 Spring 容器時,只能通過 @Bean 來實現(xiàn)。
@bean的作用范圍singleton prototype request session global session后面幾個和當初servlet作用域差不多
生命周期:init-method destroy-method
注解中@Component和@Bean的區(qū)別
實體 POJO
public class Person {
private String name;
private Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
// 省略 getter setter
// 后續(xù)會省略 toString 方法, 使用 IDE 自動生成就可以了
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}xml 配置方式
<bean id="person" class="me.sjl.bean.Person">
<property name="age" value="18"/>
<property name="name" value="sjl"/>
</bean>測試代碼
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}測試結(jié)果

bean 創(chuàng)建成功
以上是我們使用傳統(tǒng)的 xml 配置方式創(chuàng)建一個 bean 的方式,下面我們使用注解來創(chuàng)建一個 bean。
@Bean
JavaConfig 方式
@Configuration
public class BeanConfig {
@Bean
public Person person() {
return new Person("SJL01", 20);
}
}在配置類上打個一個 @Configuration 注解,表示聲明該類為 Spring 的配置類。在創(chuàng)建一個方法,方法返回對象即我們要創(chuàng)建的對象 Person , 返回該對象的實例。在方法上打上注解 @Bean即表示聲明該方法返回的實例是受 Spring 管理的 Bean。
測試代碼
@Test
public void test2() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfig.class);
Person person = ctx.getBean(Person.class);
System.out.println(person);
}結(jié)果

使用注解方法創(chuàng)建
bean
成功
這里使用的是 AnnotationConfigApplicationContext() 類,傳入?yún)?shù)就是 配置類
@Bean 注解的屬性解析
value
name
name 和 value 兩個屬性是相同的含義的, 在代碼中定義了別名。 為 bean 起一個名字,如果默認沒有寫該屬性,那么就使用方法的名稱為該 bean 的名稱
autowire
裝配方式 有三個選項
Autowire.NO (默認設(shè)置)
Autowire.BY_NAME
Autowire.BY_TYPE
指定 bean 的裝配方式, 根據(jù)名稱 和 根據(jù)類型 裝配, 一般不設(shè)置,采用默認即可
initMethod
bean 的初始化方法, 直接指定方法名稱即可,不用帶括號
@Test
public void test2() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfig.class);
Person person = ctx.getBean(Person.class);
System.out.println(person);
}Person 類添加 init() 方法
public void init() {
System.out.println("init ............");
}destroyMethod
bean 的銷毀方法, 在調(diào)用 IoC 容器的 close() 方法時,會執(zhí)行到該屬性指定的方法。不過,只是單實例的 bean 才會調(diào)用該方法,如果是多實例的情況下,不會調(diào)用該方法
@Bean(destroyMethod = "destroy")
public Person person() {
return new Person("SJL01", 20);
}Person 類添加 destroy() 方法
public void destroy() {
System.out.println(" destroy ...............");
}兩者的目的是一樣的,都是注冊bean到Spring容器中
1、@Component注解表明一個類會作為組件類,并告知Spring要為這個類創(chuàng)建bean。
2、@Bean注解告訴Spring這個方法將會返回一個對象,這個對象要注冊為Spring應(yīng)用上下文中的bean。通常方法體中包含了最終產(chǎn)生bean實例的邏輯。
區(qū)別:
1、@Component(@Controller、@Service、@Repository)通常是通過類路徑掃描來自動偵測以及自動裝配到Spring容器中。
2、而@Bean注解通常是我們在標有該注解的方法中定義產(chǎn)生這個bean的邏輯。
3、@Component 作用于類,@Bean作用于方法
Spring幫助我們管理Bean分為兩個部分
一個是注冊Bean(@Component , @Repository , @ Controller , @Service , @Configration),一個裝配Bean(@Autowired , @Resource,可以通過byTYPE(@Autowired)、byNAME(@Resource)的方式獲取Bean)。
完成這兩個動作有三種方式,一種是使用自動配置的方式、一種是使用JavaConfig的方式,一種就是使用XML配置的方式。
@Compent 作用就相當于
XML配置
@Component
public class Student {
private String name = "lkm";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}@Bean 需要在配置類中使用,即類上需要加上@Configuration注解
@Configuration
public class WebSocketConfig {
@Bean
public Student student(){
return new Student();
}
}兩者都可以通過@Autowired裝配
@Autowired Student student;
那為什么有了@Component,還需要@Bean呢?
如果你想要將第三方庫中的組件裝配到你的應(yīng)用中,在這種情況下,是沒有辦法在它的類上添加@Component注解的,因此就不能使用自動化裝配的方案了,但是我們可以使用@Bean,當然也可以使用XML配置。
到此這篇關(guān)于@Component和@Bean的區(qū)別的文章就介紹到這了,更多相關(guān)@Component和@Bean的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實現(xiàn)Excel讀取的實例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot實現(xiàn)Excel讀取的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
spring task @Scheduled注解各參數(shù)的用法
這篇文章主要介紹了spring task @Scheduled注解各參數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例
這篇文章主要介紹了java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Java實戰(zhàn)項目之斗地主和斗牛游戲的實現(xiàn)
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個斗地主和一個斗牛游戲,大家可以在過程中查缺補漏,提升水平2021-11-11
詳解SpringMVC學(xué)習(xí)系列(6) 之 數(shù)據(jù)驗證
這篇文章主要介紹了詳解SpringMVC學(xué)習(xí)系列(6) 之 數(shù)據(jù)驗證 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12

