“Spring中@Component和@Bean的异同”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
 
(未显示同一用户的10个中间版本)
第18行: 第18行:
  
 
==== 作用对象不同 ====
 
==== 作用对象不同 ====
 +
 +
===== @Component =====
 +
@Component 注解作用于类
 +
[[文件:@Component 只作用域类.png|无|缩略图|727x727像素]]
 +
 +
===== @Bean =====
 +
@Bean 注解作用于方法
 +
[[文件:@Bean 注解作用于方法.png|无|缩略图|551x551像素]]
 +
 +
 +
 +
 +
 +
  
 
==== 使用方法不同 ====
 
==== 使用方法不同 ====
 +
 +
===== @Component =====
 +
@Component 注解直接标注在类声明上面即可
 +
 +
例:<syntaxhighlight lang="java">
 +
package org.example.bean;
 +
 +
import org.springframework.stereotype.Component;
 +
 +
@Component
 +
public class Student {
 +
 +
}
 +
</syntaxhighlight>在 Spring 中 @Component 注解通常要配合包扫描来实现,所以再定义一个配置类:<syntaxhighlight lang="java">
 +
package org.example.config;
 +
 +
import org.springframework.context.annotation.ComponentScan;
 +
 +
@ComponentScan("org.example.bean")
 +
public class AppConfig {
 +
}
 +
</syntaxhighlight>配置类上面使用 @ComponentScan,@ComponentScan 需要指定 @Component 注解所在的包的路径,这样 Spring 就会扫描指定包下面所有的实体类,并把带有 @Component 注解的实体类注册到 Spring 容器中
 +
 +
 +
===== @Bean =====
 +
@Bean 需要在配置类中使用,即类上需要加上 @Configuration 注解,然后在配置类中使用一个方法来自定义 bean 是如何创建的
 +
 +
例:
 +
 +
创建实体类<syntaxhighlight lang="java">
 +
package org.example.test;
 +
 +
public class Teacher {
 +
 +
}
 +
</syntaxhighlight>
 +
然后在有 @Configuration 注解的配置类中定义一个如何产生对象的方法,然后在方法上面使用 @Bean 注解进行标识,Spring 就会将该方法返回的对象注册到 Spring 容器中<syntaxhighlight lang="java">
 +
package org.example.config;
 +
 +
import org.example.test.Teacher;
 +
import org.springframework.context.annotation.Bean;
 +
import org.springframework.context.annotation.ComponentScan;
 +
import org.springframework.context.annotation.Configuration;
 +
 +
@ComponentScan("org.example.bean")
 +
@Configuration
 +
public class AppConfig {
 +
 +
    @Bean
 +
    public Teacher teacher() {
 +
        return new Teacher();
 +
    }
 +
 +
}
 +
</syntaxhighlight>
 +
 +
 +
  
 
==== 实现不同 ====
 
==== 实现不同 ====
 +
 +
===== @Component =====
 +
@Component 注解通常是通过类路径扫描来自动侦测以及自动装配到 Spring 容器中(使用 @ComponentScan 注解定义要扫描的路径)
 +
 +
===== @Bean =====
 +
@Bean 注解通常是在标有 @Configuration 注解的方法中定义产生这个 bean,默认情况下,它将使用方法的名字作为 bean 的 id/name(类似 XML 配置:bean id=xxx)。
 +
 +
当然也可以使用 @Bean 注解的属性进行指定。
 +
 +
 +
 +
  
 
==== 灵活性不同 ====
 
==== 灵活性不同 ====
 +
@Bean 注解比 @Component 注解更灵活,我们可以按需注册需要的 bean,很多场景我们只能通过 @Bean 注解来注册 bean。
 +
 +
比如:
 +
 +
当我们引用第三方库中的类,需要装配到 Spring 容器中时,在这种情况下,是没有办法在第三方类库中的类上添加 @Component 注解的,所以我们只能通过 @Bean 来注册

2023年1月28日 (六) 22:22的最新版本

概述

@Component

表明一个类会作为组件类,并告知Spring要为这个类创建bean


@Bean

告知Spring这个方法将会返回一个对象,这个对象需要注册为Spring上下文中的bean,通常方法体中包含了最终产生bean实例的逻辑


相同点

都可以为Spring容器注册Bean对象


区别

作用对象不同

@Component

@Component 注解作用于类

生成缩略图出错:无法将缩略图保存到目标地点
@Bean

@Bean 注解作用于方法

生成缩略图出错:无法将缩略图保存到目标地点




使用方法不同

@Component

@Component 注解直接标注在类声明上面即可

例:

package org.example.bean;

import org.springframework.stereotype.Component;

@Component
public class Student {

}

在 Spring 中 @Component 注解通常要配合包扫描来实现,所以再定义一个配置类:

package org.example.config;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan("org.example.bean")
public class AppConfig {
}

配置类上面使用 @ComponentScan,@ComponentScan 需要指定 @Component 注解所在的包的路径,这样 Spring 就会扫描指定包下面所有的实体类,并把带有 @Component 注解的实体类注册到 Spring 容器中


@Bean

@Bean 需要在配置类中使用,即类上需要加上 @Configuration 注解,然后在配置类中使用一个方法来自定义 bean 是如何创建的

例:

创建实体类

package org.example.test;

public class Teacher {

}

然后在有 @Configuration 注解的配置类中定义一个如何产生对象的方法,然后在方法上面使用 @Bean 注解进行标识,Spring 就会将该方法返回的对象注册到 Spring 容器中

package org.example.config;

import org.example.test.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan("org.example.bean")
@Configuration
public class AppConfig {

    @Bean
    public Teacher teacher() {
        return new Teacher();
    }

}



实现不同

@Component

@Component 注解通常是通过类路径扫描来自动侦测以及自动装配到 Spring 容器中(使用 @ComponentScan 注解定义要扫描的路径)

@Bean

@Bean 注解通常是在标有 @Configuration 注解的方法中定义产生这个 bean,默认情况下,它将使用方法的名字作为 bean 的 id/name(类似 XML 配置:bean id=xxx)。

当然也可以使用 @Bean 注解的属性进行指定。



灵活性不同

@Bean 注解比 @Component 注解更灵活,我们可以按需注册需要的 bean,很多场景我们只能通过 @Bean 注解来注册 bean。

比如:

当我们引用第三方库中的类,需要装配到 Spring 容器中时,在这种情况下,是没有办法在第三方类库中的类上添加 @Component 注解的,所以我们只能通过 @Bean 来注册