Springboot___@SpringBootApplicaiton分析

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{};
}

可以被自动配置有很多,有如下:
列举1
列举2
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;
    //下面还有,进行省略了。因为与研究的关系不大
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值