@Bean、@Component、 @Service、 @Repository 和 @Controller注解的区别

13 篇文章 0 订阅

@Bean:表示一个方法实例化、配置或者初始化一个Spring IoC容器管理的新对象。
@Component: 自动被comonent扫描。 表示被注解的类会自动被component扫描
@Repository: 用于持久层,主要是数据库存储库。
@Service: 表示被注解的类是位于业务层的业务component。
@Controller:表明被注解的类是控制component,主要用于展现层 。

@Bean与@Component区别
@Component是 spring 2.5引入的,为了摆脱通过classpath扫描根据xml方式定义的bean的方式.

@Bean是spring 3.0 引入的,和 @Configuration一起工作,为了摆脱原先的xml和java config方式。

Spring管理Bean方式有两种,一种是注册Bean,一种装配Bean。
可以通过三种方式实现bean管理,一使用自动配置的方式、二使用JavaConfig的方式、三使用XML配置的方式。

@Compent 作用就相当于 XML配置

@Component
@Data
public class User{
    private String name = "tom";
}

@Bean 需要在配置类中使用,即类上需要加上@Component或者@Configuration注解, 通常加上@Configuration。 @Bean的用法在这里

@Configuration
public class AppConfig {

    @Bean
    public TransferServiceImpl transferService() {
        return new TransferServiceImpl();
    }
}

官方文档在这里。

两者都可以通过@Autowired装配

@Autowired
private TransferService transferService;

@Component与@Service区别
目前基本没有区别。
参看源代码spring-context-4.3.16

/**
 * Indicates that an annotated class is a "Service", originally defined by Domain-Driven
 * Design (Evans, 2003) as "an operation offered as an interface that stands alone in the
 * model, with no encapsulated state."
 *
 * <p>May also indicate that a class is a "Business Service Facade" (in the Core J2EE
 * patterns sense), or something similar. This annotation is a general-purpose stereotype
 * and individual teams may narrow their semantics and use as appropriate.
 *
 * <p>This annotation serves as a specialization of {@link Component @Component},
 * allowing for implementation classes to be autodetected through classpath scanning.
 *
 * @author Juergen Hoeller
 * @since 2.5
 * @see Component
 * @see Repository
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	String value() default "";

}

从源代码可以看出@Service和@Component没啥本质区别,官方文档也说了This annotation serves as a specialization of {@link Component @Component},

  • allowing for implementation classes to be autodetected through classpath scanning. 也就是@Service是一种具体的@Component,使得被注解类能通过classpath扫描被自动检测。
  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@Bean注解和@Component注解都是Spring框架中用来标识一个类为Bean的注解,它们有一些共同点和区别。 共同点: 1. 标识Bean:无论是@Bean注解还是@Component注解,它们都可以用来标识一个类为Spring容器中的Bean。 2. 自动注册:被@Bean注解或@Component注解标识的类会被Spring容器扫描并注册为Bean,可以在其他地方通过@Autowired注解进行注入。 3. 依赖注入:无论是@Bean注解还是@Component注解,它们都可以用于依赖注入,即在其他Bean中通过@Autowired注解引用。 区别: 1. 使用方式:@Bean注解通常用于Java配置方式中,即通过@Configuration注解的类来进行配置;而@Component注解是通用的注解,可以用于任何场景,包括Java配置方式和XML配置方式。 2. 方法级别 vs 类级别:@Bean注解通常用于方法上,表示将方法返回的对象注册为Bean;而@Component注解通常用于类级别,表示将整个类注册为Bean。但是,在使用@Component注解时,也可以用于方法上,效果与@Bean注解类似。 3. 细分注解:@Component注解还有一些派生注解,如@Service、@Repository、@Controller等,用于标识不同类型的Bean。这些派生注解在功能上与@Component注解是一样的,只是为了更好地区分和组织不同类型的Bean。 4. 参数注入:@Bean注解的方法可以接受参数,这些参数可以是其他的Bean,也可以是普通的Java对象;而@Component注解的类不能直接接受参数,需要使用构造函数或setter方法来进行注入。 总的来说,@Bean注解适用于Java配置方式,主要用于方法级别的Bean注册;@Component注解适用于通用的Bean注册场景,主要用于类级别的Bean注册,并且还有一些派生注解用于标识不同类型的Bean。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值