- @SpringBootApplication注解语义
- @SpringBootApplication属性别名
- @SpringBootApplication标注非引导类
- @SpringBootApplication“继承”@Configuration CGLIB提升特性
- 理解自动装配机制
自动装配的前提是在应用的Class Path下添加的JAR文件依赖,同时自动装配的实体并非一定装载
开发人员不需要手动配置Beans而是由Spring Boot自动装配,Spring Boot自动装配的对象是Spring Bean,不需要人工干预配置xml或者编码等形式
激活自动装配的注解@EnableAutoConfiguration和@SpringBootApplication,二选一标注在@Configuration类上
@SpringBootApplication注解语义
@SpringBootApplication用于激活@EnableAutoConfiguration、@ComponentScan和@Configuration三个注解的特性
- @EnableAutoConfiguration:激活Spring Boot自动装配机制
- @ComponentScan:激活@Component的扫描
- @Configuration:声明被标注为配置类
@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
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "nameGenerator"
)
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
由上图其实可以看出
- @SpringBootConfiguration其实就是@Configuration的别名
- @ComponentScan未使用默认值,而是排除了TypeExcludeFilter和AutoConfigurationExcludeFilter
@SpringBootApplication属性别名
@SpringBootApplication所有属性方法都标注了@AliasFor注解,用于桥接其他注解的属性
@AliasFor注解能够将一个或多个注解的属性别名在某个注解中
所以@SpringBootApplication是一个聚合注解,包含@EnableAutoConfiguration、@ComponentScan和@Configuration三个注解的核心特性
例如@RestController注解
@SpringBootApplication标注非引导类
由于@SpringBootApplication是聚合注解,所以也可以标注在配置类上,而不单单是引导类,但此时的main方法需要加载配置类
@SpringBootApplication“继承”@Configuration CGLIB提升特性
@SpringBootApplication作为@Configuration的派生注解,同样继承属性,但其最大的区别为前者有CGLIB加成
@Bean在@Component类与@Configuration类中的差异,前者是与正常的Java对象语义相同,为轻量模式(Lite),后者为完全模式(Full)会执行CGLIB提升的操作
@Configuration属于编码的方式导入,非自动装备。
其他自动装配的Bean是由自动装配机制完成的
理解自动装配机制
Spring Boot之前,Spring Framework提供Bean生命周期管理和Spring编程模型,支持注解的派生扩展,但无法自动装配@Configuration类
Spring Boot添加了约定配置化导入@Configuration类的方式
自动装配类能够打包到外部的JAR文件中,并且将被Spring Boot装载。自动装配也能被关联到“starter”中,这些“starter”提供自动装配的代码及关联的依赖
当@ConditionalOnClass标注在@Configuration类上时,当且仅当目标类存在于Class Path下时才予以装配,Spring Boot框架提供了常用的装配逻辑
spring-boot-autoconfigure是Spring Boot核心模块,提供了大量的内建自动装配@Configuration类,统一存放在org.springframework.boot.autoconfigure下,且配置累资源在META-INF/spring.factories中
即Spring Boot在其核心模块已经预先定义好了自动装配的类
创建自动配置类
- 将配置类标注@Configuration,@Import导入累
- 启动类开启@EnableAutoConfiguration
- 在项目的src/main/resource目录下新建META-INf/spring.factories资源,并配置相应的自动配置类
Spring Boot应用中,不再依赖XML配置,完全使用注解驱动进行Spring Bean的组装,注解解析带来的时间成本影响了应用的启动速度,在Spring Framework5.0引入了新注解@Indexed,在代码编译的时候,向@Component和派生注解添加索引,较少运行时性能消耗
Comments | 0 条评论