component

Spring Component annotation is used to denote a class as Component. It means that Spring framework will autodetect these classes for dependency injection when annotation-based configuration and classpath scanning is used.

Spring Component注释用于将一个类表示为Component。 这意味着当使用基于注释的配置和类路径扫描时, Spring框架将自动检测这些类以进行依赖项注入 。

弹簧组件 (Spring Component)

In layman terms, a Component is responsible for some operations. Spring framework provides three other specific annotations to be used when marking a class as Component.

用外行术语来说,组件负责某些操作。 当将一个类标记为Component时,Spring框架提供了其他三个特定的注释。

  1. Service: Denotes that the class provides some services. Our utility classes can be marked as Service classes.Service :表示该类提供了一些服务。 我们的实用程序类可以标记为服务类。
  2. Repository: This annotation indicates that the class deals with CRUD operations, usually it’s used with DAO implementations that deal with database tables.Repository :此批注表明该类处理CRUD操作,通常与用于处理数据库表的DAO实现一起使用。
  3. Controller: Mostly used with web applications or REST web services to specify that the class is a front controller and responsible to handle user request and return appropriate response.Controller :通常与Web应用程序或REST Web服务一起使用,以指定该类为前端控制器,并负责处理用户请求并返回适当的响应。

Note that all these four annotations are in package org.springframework.stereotype and part of spring-context jar.

请注意,所有这四个注释都在org.springframework.stereotype包中,并且是spring-context jar的一部分。

Most of the time our component classes will fall under one of its three specialized annotations, so you may not use @Component annotation a lot.

大多数时候,我们的组件类将属于其三个专用注释之一,因此您可能不会大量使用@Component注释。

弹簧组件示例 (Spring Component Example)

Let’s create a very simple Spring maven application to showcase the use of Spring Component annotation and how Spring autodetects it with annotation-based configuration and classpath scanning.

让我们创建一个非常简单的Spring Maven应用程序,以展示Spring Component注释的用法以及Spring如何通过基于注释的配置和类路径扫描自动检测到它。

Create a maven project and add following spring core dependency.

创建一个maven项目并添加以下spring核心依赖项。

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.6.RELEASE</version>
</dependency>

That’s all we need to get the spring framework core features.

这就是我们获取Spring框架核心功能所需要的。

Let’s create a simple component class and mark it with @Component annotation.

让我们创建一个简单的组件类,并使用@Component批注对其进行标记。

package com.journaldev.spring;import org.springframework.stereotype.Component;@Component
public class MathComponent {public int add(int x, int y) {return x + y;}
}

Now we can create an annotation based spring context and get the MathComponent bean from it.

现在,我们可以创建一个基于注释的spring上下文,并从中获取MathComponent bean。

package com.journaldev.spring;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class SpringMainClass {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.scan("com.journaldev.spring");context.refresh();MathComponent ms = context.getBean(MathComponent.class);int result = ms.add(1, 2);System.out.println("Addition of 1 and 2 = " + result);context.close();}}

Just run the above class as normal java application and you should get following output in the console.

只需将上述类作为普通的Java应用程序运行,您将在控制台中获得以下输出。

Jun 05, 2018 12:49:26 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Tue Jun 05 12:49:26 IST 2018]; root of context hierarchy
Addition of 1 and 2 = 3
Jun 05, 2018 12:49:26 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Tue Jun 05 12:49:26 IST 2018]; root of context hierarchy

Did you realized the power of Spring, we didn’t have to do anything to inject our component to spring context.

您是否意识到Spring的强大功能,我们无需执行任何操作即可将组件注入Spring上下文。

Below image shows the directory structure of our Spring Component example project.

下图显示了我们的Spring Component示例项目的目录结构。

We can also specify the component name and then get it from spring context using the same name.

我们还可以指定组件名称,然后使用相同的名称从spring上下文中获取它。

@Component("mc")
public class MathComponent {
}
MathComponent ms = (MathComponent) context.getBean("mc");
@Component annotation with MathComponent, it’s actually a service class and we should use @Component批注,但实际上它是一个服务类,我们应该使用@Service annotation. The result will still be the same.@Service批注。 结果仍然是相同的。
GitHub Repository.GitHub Repository中检出项目。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/21429/spring-component

component

component_春天@Component相关推荐

  1. Spring教程– Spring Core Framework教程

    Spring is one of the most widely used Java EE frameworks. I have written a lot on Spring Tutorial an ...

  2. vue3异步组件怎么获取ref,获取组件实例?曲线救国的方式。

    翻看文档实在没找到异步组件加载成功后的回调,获取ref写个setTimeout延迟个一两秒可以,万一网速慢就GG,所以还是得明确的知道这个组件什么时候下载引入成功的. 目前的思路是把defineAsy ...

  3. Spring中的@ Component,@ Repository和@Service批注有什么区别?

    @Repository @Component , @Repository和@Service批注可以在Spring中互换使用吗,或者除了充当注解设备外,它们还提供任何特定功能吗? 换句话说,如果我有一个 ...

  4. r中rep_Spring中@ Component,@ Service,@ Controller和@Repository之间的区别

    r中rep 在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repositor ...

  5. Spring中@ Component,@ Service,@ Controller和@Repository之间的区别

    在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repository批注之间的 ...

  6. 春季:@Component与@Bean

    本文翻译自:Spring: @Component versus @Bean I understand that @Component annotation was introduced in spri ...

  7. ❤️关于 idea 安装 Vue 插件后新建文件不显示 Vue Component 的问题及解决方法❤️

    关于 idea 安装 Vue 插件后新建文件不显示 Vue Component 的问题及解决方法 1.点击file打开设置settings,打开Editor找到 file and code templ ...

  8. Spring5春天还是配置地狱

    # Spring笔记 ## 1.概述及IOC理论推导 #### 1.1 Spring简介 Spring : 春天 --->给软件行业带来了春天 2002年,Rod Jahnson首次推出了Spr ...

  9. React router 的 Route 中 component 和 render 属性理解

    React router 的 Route 中 component 和 render 属性理解 Route 标签的三个互斥属性 render.component.children Route 就是用来匹 ...

最新文章

  1. 鸿蒙不算安卓系统吗,加入鸿蒙≠使用鸿蒙,鸿蒙不属于华为也仅限于华为
  2. css修改同步保存到文件,谷歌浏览器修改CSS和js后同步保存到文件中 (译)
  3. SMS短信的C语言代码摘抄
  4. VSS2005 上传pdf 空白
  5. 解决 idea 运行 Spring Boot 项目启动慢的问题
  6. python json转dict(dict转json)
  7. 深度链接对社会化营销有哪些价值和作用?
  8. 去除残缺条目java_为什么说Java匿名内部类是残缺的闭包
  9. 从外部访问Kubernetes集群中的应用
  10. 智能语音识别转文字字幕实时上屏直播系统
  11. RAID磁盘阵列的部署(red hat 6.5)
  12. BigDecimal 使用浅析
  13. 针对IE安全控件的爬虫
  14. qq表情包html插件,程序员用jQuery写QQ表情插件,代码思路瞬间爆炸
  15. react 项目使用qrcode.react生成二维码,并提供批量下载
  16. 通过LL库初始化STM32的硬件IIC
  17. 算术,逻辑左移右移(转)
  18. 中科院1区JGG微生物专刊征稿
  19. linux系统设置密钥登录
  20. 浪漫表白c语言程序玫瑰,c语言玫瑰告白代码

热门文章

  1. 无法Debug SQL: Unable to start T-SQL Debugging. Could not attach to SQL Server process on
  2. SIM300命令参考
  3. 匹马行天下之思维决定高度篇——教你如何爱上“编程妹子”
  4. 类型xxx 无法反序列化。缺乏对应的数据成员。
  5. iOS直播集成和问题总结(阿里云直播)
  6. 【Python3】待解决的疑问
  7. 运用GRASP原则来做uml交互类图-------pos机实例
  8. BSD Socket~TCP~Example Code
  9. App.config 中读写appSettings、system.serviceModel终结点,以及自定义配置节
  10. python--之np.any,np.a;;