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是作用在方法参数上,用于将前台