【SpringBoot】15. Spring Boot核心注解
Spring Boot核心注解
1 | @SpringBootApplication | 代表是Spring Boot启动的类 |
---|---|---|
2 | @SpringBootConfiguration | 通过bean对象来获取配置信息 (被@Configuration修饰所以才能够获取配置信息) |
3 | @Configuration | 通过对bean对象的操作替代spring中的xml文件 |
4 | @EnableAutoConfiguration | 完成一些初始化环境的配置 |
5 | @ComponentScan | 完成spring的组件扫描。替代之前在xml文件中配置组件扫描的配置(context:component-scan pacage=”....”) |
6 | @RestController | 1,表示一个 Controller。2,表示当前这个 Controller 下的所有的方法都会以 json 格式的数据响应。 和 @Controller + @ResponseBody组合使用一样。 |
1.@SpringBootApplication —— Spring Boot启动的类
@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 {
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {};
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
String[] excludeName() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
其中有3个注解是我们所关心的:
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
2.@SpringBootConfiguration —— 通过bean对象来获取配置信息
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
这个注解是空的,什么都没有,但是他被另一个注解所修饰——@Configuration
3.@Configuration —— 通过对bean对象的操作替代Spring中的xml文件
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
4.@EnableAutoConfiguration —— 根据导入的jar包创建启动环境
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {};
}
5.@ComponentScan —— 完成spring的组件扫描
代替了xml文件中的:
<context:component-scan pacage=”....”></context:component-scan>
6.@RestController —— 一个Controller 且该controller下的所有数据以json格式的数据响应
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
String value() default "";
}
他被@Controller 和 @ResponseBody 同时修饰 , 因此@RestController = @Controller + @ResponseBody
本文来自博客园,作者:邓晓晖,转载请注明原文链接:https://www.cnblogs.com/isdxh/p/13529219.html