博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot入门--自动配置
阅读量:3888 次
发布时间:2019-05-23

本文共 2228 字,大约阅读时间需要 7 分钟。

SpringBoot入门–自动配置

主程序类

/*来标注一个主程序类,说明这是一个springboot程序*/import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class HelloworldMainApplication {public static  void main(String[] args) {    SpringApplication.run(HelloworldMainApplication.class, args);}}

@SpringBootApplication:SpringBoot应用标注在某个类上,就说明哪个类是SpringBoot是主配置类,SpringBoot就启动这个类的main方法来启动应用;

@SpringBootApplication是一个复合注解

import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.context.TypeExcludeFilter;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.core.annotation.AliasFor;@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})})

包括

  1. @ComponentScan
  2. @SpringBootConfiguration
  3. @EnableAutoConfiguration

@EnableAutoConfiguration

作用:
它主要利用了一个
EnableAutoConfigurationImportSelector选择器给Spring容器中来导入一些组件

3.1 @AutoConfigurationPackage   自动配置包   将主配置类的所在包以及下面所有子包里的插件扫描进spring容器   3.2@Import({EnableAutoConfigurationImportSelector.class})  Spring底层注解import,**关键功能是Import注解**

@SpringBootConfiguration继承自@Configuration,二者功能也一致,一个为spring注解,一个为SpringBoot注解,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。

@EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web

,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。

@ComponentScan,扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的context:component-scan(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。所以本demo中的User为何会被spring容器管理。

转载地址:http://lqthn.baihongyu.com/

你可能感兴趣的文章
Android canvas rotate():平移旋转坐标系至任意原点任意角度-------附:android反三角函数小结...
查看>>
Matlab读取avi视频并播放 你必须要知道的
查看>>
word字体大小与公式编辑器字体对照表
查看>>
visio画图-----如何克服两箭头交叉变形 及 箭头自动重绘?
查看>>
Android开发:安装NDK,移植OpenCV2.3.1,JNI调用OpenCV全过程
查看>>
“金9银10”2020年JVM高频率面试题整理,技术提升就差一个点!
查看>>
简简单单的分享2020常见的MySQL面试题MySQL与答案整理
查看>>
听说只有大厂的Android工程师才能全答对这20道题?我看你在吹牛哦!
查看>>
武功秘籍之 Redis 面试题全掌握,学完马上找面试官对线!
查看>>
50道!2020年!!MySQL高频数据库面试题解析,你都懂了吗?
查看>>
如何用Spring Boot加密配置文件中的特殊内容示例代码详解
查看>>
谈谈这些年面试官给大伙下的那些套,如何解?(面试技巧)
查看>>
5年开发经验的我被几条朋友圈打击到,点燃自己冲击阿里面经!
查看>>
5年工作经验的我放弃安逸,一份来自腾讯魔鬼面试的终极考验!
查看>>
学JAVA吗同学,这篇Sping boot 确定不了解下么?
查看>>
(3年+offer)华为技术岗面试初面+综合面试经验总结
查看>>
男默女泪,努力复习的我终于通过社招进入BAT工作了!(JAVA+JVM+框架+中间件+Spring干货分享)
查看>>
Python 导包
查看>>
dok_matrix
查看>>
theano 后端爆内存
查看>>