1. @SpringBootApplication注解分析
Springboot项目一般都会有*Application
的入口类,然后入口类中会有一个mian入口方法。而注解@SpringBootApplication
作为SpringBoot的核心注解,它是个组合注解,我们有必须来了解一下。
我们来看下@SpringBootApplication注解的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
//该注解表明这个注解应该被javadoc工具记录,注解类型信息也会被记录在生成的文档中。
//默认情况下,javadoc是不会讲注解记录下来的
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(exculdeFilter = @Filter(
type = FilterType.CUSTOM, classes = TypeExculdeFilter.class))
public @interface SpringBootApplication{
Class<?>[] exclude() default{};
String[] excludeName() default{};
//@AliasFor注解是起别名的作用
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default{};
//@AliasFor注解是起别名的作用
@AliasFor(annotation = ComponentScan.class, attribute ="basePackageClasses")
Class<?>[] scanBasePackages() default{};
}
2.拆分分析之@SpringBootConfiguration
@SpringBootConfiguration
是springboot项目的配置注解,同时也是一个组合注解。在Springboot项目中,推荐使用@SpringBootConfiguration来代替@Configuration注解。
其配置如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration{
}
说明:springboot的项目必须要将parent设置为springboot的parent,该parent包含了大量默认的设置,大大简化了我们的开发。
3. 拆分分析之@EnableAutoConfiguration
@EnableAutoConfiguration
这个注释的作用是启用自动配置,该注解会使SpringBoot根据项目中依赖的jar包自动配置项目的配置项:
举个例子来说,我们如果添加了spring-boot-starter-web的依赖,那么项目中也会引入SpringMVC的依赖,SpringBoot就会自动配置tomcat和SpringMVC。
@EnableAutoConfiguration
的源代码如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
//@Inherited阐述了某个被标注的类型是被继承的。有了该注解,子类可以获取到父类的注解信息
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfigration{
String ENABLE_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?> exclude() defualt{};
String[] excludeName() default{};
}
可以被自动配置有很多,有如下:
4. 注解说明
我们来看下一个简单的springboot应用基本的配置:
如果我们要自己设置某些配置的话需要做如下的改动:
//在启动入口*Application中的注解@SpringBootApplicaiton设置
@SpringBootApplication(exclude = {RedisAutoConfiguration.class})
public class *Applicaiton{
}
@Controller
@SpringBootApplication
@Configuration
public class HelloApplication{
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "hello word!";
}
public static void main(String[] args){
SpringApplication.run(HelloApplication.class,args);
}
}
注解解释:
<1>@Controller
:代表该类是一个Handler
<2>@SpringBootAppliction
:代表该类是一个SpringBoot应用
<3>@Configuration
:代表该类是一个java配置类
4. 拆分分析之@ComponentScan
@ComponentScan
的作用是进行扫描,而其动作是默认扫描@SpringBootApplicaiton
所在类的同级目录以及它的子目录。
componentScan的源码如下:
@Retention(RetetionPolicy.RUNTIME)
@Target(ElementType.RUNTIME)
@Documented
//被Repeatable修饰的注解可以重复的修饰某个元素
//T^T,有点拗口,我也没去深入了解
@Repeatable(ComponentScan.class)
public @interface ComponentScan{
@AliasFor("basePackages")
Sting[] value() default {};
@AliasFor("value")
String[] basePackages default {};
Class<?>[] basePackagesClasses() default {};
Class<? extends BaseNameGenerator> nameGenerator()
default BeanNameGenerator.class;
Class<? extends ScopeMetadataReslver> scopeResolver()
default AnnotationScopeMetadataResolver.class;
//下面还有,进行省略了。因为与研究的关系不大
}