Spring Boot 常用注解及意义

@Controller

  • 处理http请求

以前在编写Controller方法的时候,需要开发者自定义一个Controller类实现Controller接口,实现handleRequest方法返回ModelAndView。并且需要在Spring配置文件中配置Handle,将某个接口与自定义Controller类做映射。

这么做有个复杂的地方在于,一个自定义的Controller类智能处理一个单一请求。而在采用@Contoller注解的方式,可以使接口的定义更加简单,将@Controller标记在某个类上,配合@RequestMapping注解,可以在一个类中定义多个接口,这样使用起来更加灵活。

被@Controller标记的类实际上就是个SpringMVC Controller对象,它是一个控制器类,而@Contoller注解在org.springframework.stereotype包下。其中被@RequestMapping标记的方法会被分发处理器扫描识别,将不同的请求分发到对应的接口上。

使用时注意:@RestController 是返回json数据,@Controller是返回页面。

@SpringBootApplication

该注解是启动Spring Boot应用的注解,实际上它是一个复合Annotation。其实现如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    ......
}

这其中我们需要关注的,只有三个Annotation:

@SpringBootConfigration

@EnableAutoConfiguration

@ComponentScan

如果我们使用以上三个annotation替换@SpringBootAnnotation,则与之前功能对等。

1)@SpringBootConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
 
}

该注解继承自@Configuration,两者的功能也一致,就是标注当前类为一个配置类。会将当前类声明的一个或多个以@Bean注解标记的方法实例纳入到Spring容器中,并且实例名就是方法名。

其父注解@Configuration就是JavaConfig形式的Spring Ioc容器配置类使用的那个@Configuration。Spring Boot启动时,需要加载的IoC容器的配置即由此定义。

2)@EnableAutoConfiguration
@EnableAutoConfiguration的理念就是借助@Import的支持,收集和注册特定场景相关的Bean的定义。比如:

  • @EnableScheduling是通过@Import将Spring调度框架相关的Bean定义加载到IoC容器。
  • @EnableCaching是通过@Import将Spring缓存框架(如Guava等)相关的Bean定义加载到IoC容器。

而@EnableAutoConfiguration也是借助@Import的帮助,将所有复合自动配置条件的Bean定义加载到IoC容器。仅此而已。

该注解定义如下:

@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    ......
}

其中最关键的要属@Import(EnableAutoConfigurationImportSelector.class),借助EnableAutoConfigurationImportSelector这个类,@EnableAutoConfiguration可以帮助Spring Boot应用将符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。

具体的加载方式是通过SpringFactoriesLoader从指定的配置文件:META-INF/spring.factories加载配置。

所以,@EnableAutoConfiguration自动配置就变成了:从classpath中搜寻所有的META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的配置项通过反射实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,然后汇总并加载到IoC容器中。

3)@ComponentScan
@ComponentScan的功能其实就是自动扫描并加载符合条件的组件或Bean定义,并最终将这些Bean定义加载到容器中。其作用是:

  • 自动扫描路径下边带有@Controller,@Service,@Repository,@Component注解加入Spring IoC容器。
  • 通过includeFilters加入扫描路径下没有以上注解的类加入spring容器。
  • 通过excludeFilters过滤出不用加入spring容器的类。

@Autowired

@Autowired是Spring 3.0引入的一个注解,可以标注在类的属性上,这样Spring容器就会以byType的方式来注入对应的Bean。

如果某个Type实现了多个Bean,则使用@Autowired时,需要根据@Qualifier注解来指定需要注入哪个实现类。也可以采用List或Map的方式将所有实现类一起注入。如果注入的是Map,则key为该Bean的id。

@Component

@Component用于将类实例化并注入IoC中(把普通类实例化到spring容器中)。其他注解@Controller,@Service,@Respository的用法跟@Component差不多,比@Component带有更多的语义,他们分别对应了控制层,业务层和持久层的类。
@Component和@Bean对比
1)@Component注解表明一个类会作为组件类,并告知Spring要为这个类创建bean,@Component(@Controller、@Service、@Repository)通常是通过类路径扫描来自动侦测以及自动装配到Spring容器中。
2)@Bean注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。通常方法体中包含了最终产生bean实例的逻辑,并且实例名就是方法名。
总结:@Component和@Bean都是用来注册Bean并装配到Spring容器中,但是Bean比Component的自定义性更强。可以实现一些Component实现不了的自定义加载类

二 Spring Boot Web相关注解

@RequestMapping

@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。 该注解有六个属性:

  • params:指定request中必须包含某些参数值是,才让该方法处理。
  • headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。
  • value:指定请求的实际地址,指定的地址可以是URI Template 模式
  • method:指定请求的method类型, GET、POST、PUT、DELETE等
  • consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html;
  • produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回

@ResponseBody

使用@ResponseBody表示该方法的返回结果直接写入HTTP response body中。

猜你喜欢

转载自blog.csdn.net/a290575479/article/details/106836131