SpringBoot_2(主类注解)

SpringBoot_2

一、解析主类—注解
主类:

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.wangxing.springboot")
public class Springbootdemo1Application {
    public static void main(String[] args) {
  	SpringApplication.run(Springbootdemo1Application.class, args);
    }
}

1.@SpringBootApplication注解

源码:

@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 {
........
}

@SpringBootApplication实际上它是一个复合注解。
虽然它的定义使用了多个注解,但实际上对于 SpringBoot 应用来说,重要的只有三个 注解,这三个注解:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
      @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

所以,如果我们使用上面三个主解作用在主类上整个 SpringBoot 应用依然可以与之前的启动类功能对等。
例如:

package com.wangxing.springboot.springbootdemo2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public class Springbootdemo2Application {
    public static void main(String[] args) {
        SpringApplication.run(Springbootdemo2Application.class, args);
    }
}

但每次都写三个注解显然过于繁琐,所以写一个 @SpringBootApplication 这样的一站式复合注解显然更方便些。

2.@SpringBootConfiguration注解 [创建配置类从而代替配置文件]
@SpringBootConfiguration注解中包含了@Configuration。

@Configuration–是 Spring JavaConfig 形式的 Spring IoC 容器的配置类使用的注解。

配置方式:

1.基于 XML 的配置方式。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!-- bean定义 -->
</beans>

2.基于 JavaConfig 的配置方式

@Configuration
public class MyConfiguration{
// bean定义
} 

任何一个标注了 @Configuration 的 Java 类定义都是一个 JavaConfig 配置类。

1.基于 XML 的注册 bean 定义方式。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!-- bean定义 -->
<bean id=””  class=””></bean>
</beans>

2.基于JavaConfig 的注册 bean 定义方式。

@Configuration
public class MyConfiguration{
// bean定义
 	@Bean
    public StudentService  studentService() {
        return new StudentServiceImpl();
    }
} 

任何一个标注了 @Bean 的方法,其返回值将作为一个 bean 定义注册到 Spring 的 IoC 容器,方法名将默认成为该 bean 定义的 id。

1.基于 XML 的依赖注入方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!-- bean定义 -->
<bean id=””  class=””>
<property name="studentDao" ref="studentDao" />
</bean>
</beans>

2.基于JavaConfig 的依赖注入方式。

@Configuration
public class MyConfiguration{
// bean定义
 	@Bean
    public StudentService  studentService() {
        return new StudentServiceImpl(studentDao() );
}

	@Bean
    public  StudentDao  studentDao() {
        return new StudentDaoImpl();
}
} 

如果一个 bean 的定义依赖其他 bean,则直接调用对应 JavaConfig 类中依赖 bean 的创建方法就可以了。
总结:
@SpringBootApplication中包含了@SpringBootConfiguration注解,@SpringBootConfiguration注解中包含了@Configuration注解,@Configuration注解标注在哪一个java类上,那么这个java类就是一个 JavaConfig 配置类,这个JavaConfig 配置类可以代替掉Spring配置文件【applicationContext.xml】,当我们在主类上标注@SpringBootApplication时,意味着主类是一个JavaConfig 配置类,因此我们在创建SptingBoot项目的时候才不用去编写Spring配置文件【applicationContext.xml】。

3.@EnableAutoConfiguration [“智能”的完成自动配置]

@EnableAutoConfiguration 中包含了@Import注解
@Import注解将bean对象注册到javaConfig配置类中。
@EnableAutoConfiguration注解利用@Import注解收集和注册特定模块相关的 bean 定义

源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
......
}

@Import(AutoConfigurationImportSelector.class).借助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration 可以帮助 SpringBoot 应用将所有符合条件的 @Configuration 配置都加载到当前 SpringBoot 创建并使用的 IoC 容器。
在这里插入图片描述

借助于 Spring 框架原有的一个工具类:SpringFactoriesLoader 的支持,@EnableAutoConfiguration 可以“智能”地自动配置功效才得以大功告成!

4.@ComponentScan注解
作用:
配置自动扫描包,就可以让类上带有@Component 和 @Repository,@Service注解的java类创建出对象。

@ComponentScan 对应 XML 配置形式中的 <context:component-scan> 元素,用于配合一些元信息注解,比如 @Component 和 @Repository 等,如果不指定,则默认 Spring 框架实现会从声明 @ComponentScan 所在类的 package 进行扫描。

如果我们当前应用没有任何 bean 定义需要通过 @ComponentScan 加载到当前 SpringBoot 应用对应使用的 IoC 容器,那么,除去 @ComponentScan 的声明,当前 SpringBoot 应用依然可以照常运行,功能对等。

二、SpringBoot核心自动配置原理
1.SpringBoot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration。

2.@EnableAutoConfiguration注解利用@Import(AutoConfigurationImportSelector.class)中的参数EnableAutoConfigurationImporttSelector类给SpringIOC容器中导入一些组件。

3.每一个自动配置类进行自动配置功能。

4.根据当前不同的条件判断,决定这个配置类是否生效,一旦这个配置类生效,这个配置类就会给容器中添加各种组件,这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的。

5.所有在配置文件中能配置的属性都是在xxxxPropertites类中封装着,配置文件能配置什么就可以参照某个功能对应的这个属性类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值