SpringBoot注解记录日记

本文详细介绍了SpringBoot中的关键注解,如@SpringBootApplication、@RestController、@Component、@Configuration及其衍生注解。@SpringBootApplication整合了@Configuration、@EnableAutoConfiguration和@ComponentScan,用于启动SpringBoot应用。@RestController用于处理HTTP请求并返回JSON数据。@ComponentScan用于扫描组件。@Configuration注解用于定义配置类。此外,还讨论了统一异常处理@ControllerAdvice、导入类@Import以及Lombok的@UtilityClass,以及缓存注解@CacheEvict、@Cacheable和@CachePut的使用和区别。
摘要由CSDN通过智能技术生成

Spring中的注解包含以下内容:

1、@SpringCloudApplication 微服务启动类


@SpringCloudApplication时一个注解的集合,其中包含@SpringBootApplication。

注解@SpringCloudApplication包括:@SpringBootApplication、@EnableDiscoveryClient、@EnableCircuitBreaker,分别是SpringBoot注解、注册服务中心Eureka注解、断路器注解。对于SpringCloud来说,这是每一微服务必须应有的三个注解,所以才推出了@SpringCloudApplication这一注解集合。

2、@SpringBootApplication 启动类注解


@SpringBootApplication注解一般放在项目的一个启动类上,用来把启动类注入到容器中,用来定义容器扫描的范围,用来加载classpath环境中一些bean。

  • @SpringBootApplication注解默认只扫描该注解注解类所在的包,如果扫描上层目录的,需要配置注解中的scanBasePackages属性显示指定要扫描的包的范围。
@SpringBootApplication     /** 可选项(scanBasePackages="com.example")*/
public class AgricultureApplication {
    public static void main(String[] args) {
        SpringApplication.run(AgricultureApplication.class, args);
    }
}

因为,从@SpringBootApplication的源码中可以看出@SpringBootApplication注解主要包装了@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan注解。

@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 {
  • @SpringBootConfiguration 主要用来把bean注入到容器中,因为该注解又封装了@Configuration注解。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}

而@Configuration注解又封装了@Component注解,@Component注解主要用来把一个bean注入到容器中。

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

}

因此,@SpringBootConfiguration注解就拥有了@Component注解的功能,用来把一个bean注入到容器中。

  • @EnableAutoConfiguration 这个注释告SpringBoot“猜”你将如何想配置Spring,基于你已经添加jar依赖项。如果spring-boot-starter-web已经添加Tomcat和Spring MVC,这个注释自动将假设您正在开发一个web应用程序并添加相应的spring设置。自动配置被设计用来和“Starters”一起更好的工作,但这两个概念并不直接相关。您可以自由挑选starter依赖项以外的jar包,springboot仍将尽力自动配置您的应用程序。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {}

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

详细介绍SpringBoot之@EnableAutoConfiguration注解

  • @ComponentScan 注解主要用来指定扫描容器的范围。

@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中

3、@RestController


常见的Controller层写法

@Api(tags = {"Controller层"})
@RestController
@RequestMapping("/farmer")
public class FarmerController {}

@RestController 是@Controller 和@ResponseBody

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {}
  • @Controller是一个@Component注解。

  • @ResponseBody的作用其实是将java对象转为json格式的数据。@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回【xml格式 或者 json等】数据。

  • @RequestBody是作用在方法参数上,用于将前台

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值