Spring注解篇,学完注解深入了解SpringBoot更容易

由于Spring Boot项目底层也都是Spring,使用Spring Boot就需要对Spring的注解有一定的了解,这次就把Spring的部分注解聊一下。熟悉了Spring的注解使用Spring Boot开发更是得心应手。

@ComponentScan:用于指定扫描包的路径,只有在它指定的包下 的类,才能归Spring管理。

Provides support parallel with Spring XML's {@code <context:component-scan>} element。
ComponentScan 这个注解类似于XML配置的context:component-scan的功能

@Bean在不指定Scope的情况下,所有的Bean都是单实例的,而且是饿汉加载,也就是容器启动的时候实例就创建好了。

指定Scope为prototype的时候表示多实例的,而且是饿汉加载(容器启动的时候不会创建,只有在第一次使用的时候才创建。这个@Bean注解常常和@Configuration注解一起使用(PS想更详细了解Configuration这个配置类注解请看我的这篇文章)

@Contional 进行条件判断的注解。(这个注解SpringBoot底层用的多,我们如果写公共的jar特别是自己写Starter的时候会常用到,自己功能开发的时候用的不多)

Contional:有条件的Spring中的注释如下 :Indicates that a component is only eligible for registration when all match
加了Contional。表示组件只有满足Contional的判断条件才有资格注入Spring容器Indicates:表示,表明。eligible:有资格的

@Component :Java类加上这个注解这个类就归Spring容器管理了,代表它是Spring的一个组件。(PS:加注解的类需要在ComponentScan包扫描的范围内),还有开发中常常用到的@Controller,@Service,@Configuration也都属于Component,如下所示:

@Component
public @interface Configuration 
@Component
public @interface Controller 
@Component
public @interface Service {
上面3个类上面都加了Component注解,他们标注的注解都是Spring的组件
当然还有其他的注解也都加了Component,就不一一列举了。

component: 组成部分;成分;部件Spring 中的描述 Indicates that an annotated class is a "component".
加了注解的类代表是一个组件

@Autowired 默认是按照类型匹配的方式。

容器中查找匹配的bean,当且仅有一个匹配的bean时。Spring将其注入到@Autowired所标注的变量中。如果容器中有一个以上匹配的bean时,则可以通过@Qualifier注解限定bean的名称。(PS一般开发的时候一个接口只有一个实现类,当写的有2个的时候可以用这种方式完成注入不报错)。

Autowired:自动装配In case of a {@link java.util.Collection} or {@link java.util.Map} dependency type, the container autowires all beans matching the declared value type. For such purposes, the map keys must be declared as type String which will be resolved to the correspondingbean names. 

大概解释就是当容器中存在多个同一种类型的要注入的时候,需要指定名称。PS:Qualifier这个刚好可以指定名称。

@EnableConfigurationProperties 有2个作用如下:

1:对类的配置绑定的功能。

2:把这个类自动注入的容器中。

这个注解的作用就是使 @ConfigurationProperties生效,如果一个配置类只配置@ConfigurationProperties注解,而没有配置@Component,那么在IOC容器中是获取不到properties配置转换的bean的 。

@EnableConfigurationProperties 注解相当于把使用 @ConfigurationProperties 注解的类进行了一次注入。(PS有的开发者就有疑问说:想把类注入到Spring容器的把类加上@Component不就可以了。但有的时候我们引入的是其他的jar包我们自己无法直接加@Component注解的,所以需要用到EnableConfigurationProperties这个注解),Spring官方也是非常支持开发者这样做的。

enable:使能够。properties:特性property的复数

/**
 * Enable support for {@link ConfigurationProperties}
 * annotated beans.{@link ConfigurationProperties} beans 
 * can be registered in the standard way (for example 
 * using {@link Bean @Bean} methods) or, for convenience, 
 * can be specified directly on this annotation.
 * 这个注解Spring 也是推荐结合 ConfigurationProperties注解一起使用
 */
@Import(EnableConfigurationPropertiesImportSelector.class)
public @interface EnableConfigurationProperties {

@Import :上一篇文章讲了它的2个作用,这个注解还有第三个作用是导入

ImportBeanDefinitionRegistrar然后我们可以实现手动注入bean到Spring容器(用法写一个类实现ImportBeanDefinitionRegistrar,然后重写实现类的方法就可以帮助我们注入相关的bean)。如下

 

public @interface Import {
  /**
   * {@link Configuration}, 
   * {@link ImportSelector}, 
   * {@link ImportBeanDefinitionRegistrar},第三个作用导入
   * 的实现类 ImportBeanDefinitionRegistrar
   * or regular component classes to import.
   */
  Class<?>[] value();

这篇文章只聊注解的作用,没说实际使用,希望都能看得懂,也希望写的这些对你能有部分作用。

 

posted @ 2023-02-25 13:33  程序员xiaozhang  阅读(327)  评论(0编辑  收藏  举报