【SpringBoot】的核心配置和运行原理
2018-03-13 21:50
1126 查看
1.基本配置
1.1入口类
Spring Boot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法其实就是一个标准的Java应用的入口方法。在main方法中使用SpringApplication.run(HelloworldApplication.class, args),启动SpringBoot应用项目。
1.2@SpringBootApplication
是核心注解,它是一个组合注解,主要组合了@Configuration、@EnableAutoConfiguration、@ComponentScan。若不使用@SpringBootApplication注解,则可以在入口类上直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan。其中@EnableAutoConfiguration让SpringBoot根据类路径中的jar包依赖为当前项目进行自动配置(如添加了spring-boot-starter-web依赖,会自动添加Tomcat和SpringMVC的依赖,并进行自动配置)。
1.3关闭特定的自动配置
使用@SpringBootApplication注解的exclude = {DataSourceAutoConfiguration.class},例如@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
1.4定制Banner
在src/main/resources下新建一个banner.txt,通过http://patorjk.com/software/taag网站生成字符,将网站生成的字符复制到banner.txt中。
1.5Spring Boot的配置文件
使用一个全局配置文件application.properties(对一些默认配置的配置值进行修改)或application.yml,放置在src/main/resources目录或类路径的/config下。将Tomcat的默认端口号8080修改为8090,并将默认的访问路径“/”修改为“/hello”,可以在application.properties中添加:
server.port=8090
server.servlet.context-path=/hello
1.6starter pom
下面为官方提供的一些starter pom
spring-boot-starter SpringBoot核心starter,包含自动配置、日志、yaml配置文件的支持
spring-boot-starter-actuator 准生产特性,用来监控和管理应用
spring-boot-starter-remote-shell 提供基于ssh协议的监控和管理
spring-boot-starter-amqp 使用spring-rabbit来支持AMQP
spring-boot-starter-aop 使用spring-aop和AspectJ支持面向切面编程
1.7使用xml配置
提倡零配置,即无xml配置,但是在实际
bab5
项目中,可能有一些特殊要求你必须使用xml配置,可以通过Spring提供的@ImportResource来加载xml。例如@ImportResource({“classpath:some-context.xml”,”classpath:another-context.xml”})
2.外部配置
2.1常规属性配置
在常规Spring环境下,注入properties文件里的值的方式,通过@PropertySource指明properties文件的位置,然后通过@Value注入值。在SpringBoot只需要在application.properties定义属性,直接使用@Value注入即可。
修改入口类代码
2.2类型安全的配置(基于properties)
通过@ConfigurationProperties将properties属性和一个bean及其属性关联,从而实现类型安全的配置。
类型安全的Bean
控制器类的代码
运行结果
3.日志配置
Spring Boot支持Logging、Log4J、Logback作为日志框架,无论使用那种日志框架,SpringBoot已为当前使用日志框架的控制台输出及文件输出做好了配置。默认情况下,使用Logback作为日志框架。
4.Profile配置
Profile是Spring用来针对不同的环境对不同的配置提供支持的,全局Profile配置使用application-{profile}.properties(如application-prod.properties),通过在application.properties中设置spring.profile.active=prod 来指定活动的Profile。
下面将做一个简单的演示,分为生产(prod)和开发(dev)环境,生产环境下端口号为80,开发环境下端口号为8888。
生产环境和开发环境的配置文件如下:
目录结构
在application.properties中添加
启动程序,结果为
5.SpringBoot运行原理
在application.properties中设置 debug=true ,可以查看当前项目中已启动或未启动的自动配置的报告。未启动的自动配置如下
运作原理
关于SpringBoot的运作原理,还是回归到@SpringBootApplication(组合注解,核心功能由@EnableAutoConfiguration注解提供)注解上@EnableAutoConfiguration注解的关键功能是@Import注解导入的配置功能,EnableAutoConfigurationImportSelector使用SpringFactoriesLoader.loadFactoryNames方法来扫描具有META-INF/spring.factories文件的jar包,此文件中声明了哪些自动配置。
1.1入口类
Spring Boot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法其实就是一个标准的Java应用的入口方法。在main方法中使用SpringApplication.run(HelloworldApplication.class, args),启动SpringBoot应用项目。
1.2@SpringBootApplication
是核心注解,它是一个组合注解,主要组合了@Configuration、@EnableAutoConfiguration、@ComponentScan。若不使用@SpringBootApplication注解,则可以在入口类上直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan。其中@EnableAutoConfiguration让SpringBoot根据类路径中的jar包依赖为当前项目进行自动配置(如添加了spring-boot-starter-web依赖,会自动添加Tomcat和SpringMVC的依赖,并进行自动配置)。
1.3关闭特定的自动配置
使用@SpringBootApplication注解的exclude = {DataSourceAutoConfiguration.class},例如@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
1.4定制Banner
在src/main/resources下新建一个banner.txt,通过http://patorjk.com/software/taag网站生成字符,将网站生成的字符复制到banner.txt中。
1.5Spring Boot的配置文件
使用一个全局配置文件application.properties(对一些默认配置的配置值进行修改)或application.yml,放置在src/main/resources目录或类路径的/config下。将Tomcat的默认端口号8080修改为8090,并将默认的访问路径“/”修改为“/hello”,可以在application.properties中添加:
server.port=8090
server.servlet.context-path=/hello
1.6starter pom
下面为官方提供的一些starter pom
spring-boot-starter SpringBoot核心starter,包含自动配置、日志、yaml配置文件的支持
spring-boot-starter-actuator 准生产特性,用来监控和管理应用
spring-boot-starter-remote-shell 提供基于ssh协议的监控和管理
spring-boot-starter-amqp 使用spring-rabbit来支持AMQP
spring-boot-starter-aop 使用spring-aop和AspectJ支持面向切面编程
1.7使用xml配置
提倡零配置,即无xml配置,但是在实际
bab5
项目中,可能有一些特殊要求你必须使用xml配置,可以通过Spring提供的@ImportResource来加载xml。例如@ImportResource({“classpath:some-context.xml”,”classpath:another-context.xml”})
2.外部配置
2.1常规属性配置
在常规Spring环境下,注入properties文件里的值的方式,通过@PropertySource指明properties文件的位置,然后通过@Value注入值。在SpringBoot只需要在application.properties定义属性,直接使用@Value注入即可。
application.properties #添加常规属性配置 book.author=jello book.name=spring boot
修改入口类代码
2.2类型安全的配置(基于properties)
通过@ConfigurationProperties将properties属性和一个bean及其属性关联,从而实现类型安全的配置。
application.properties #添加属性配置 book.name=springboot book.author=jello book.price=28
类型安全的Bean
控制器类的代码
运行结果
3.日志配置
Spring Boot支持Logging、Log4J、Logback作为日志框架,无论使用那种日志框架,SpringBoot已为当前使用日志框架的控制台输出及文件输出做好了配置。默认情况下,使用Logback作为日志框架。
4.Profile配置
Profile是Spring用来针对不同的环境对不同的配置提供支持的,全局Profile配置使用application-{profile}.properties(如application-prod.properties),通过在application.properties中设置spring.profile.active=prod 来指定活动的Profile。
下面将做一个简单的演示,分为生产(prod)和开发(dev)环境,生产环境下端口号为80,开发环境下端口号为8888。
生产环境和开发环境的配置文件如下:
目录结构
在application.properties中添加
启动程序,结果为
5.SpringBoot运行原理
在application.properties中设置 debug=true ,可以查看当前项目中已启动或未启动的自动配置的报告。未启动的自动配置如下
运作原理
关于SpringBoot的运作原理,还是回归到@SpringBootApplication(组合注解,核心功能由@EnableAutoConfiguration注解提供)注解上@EnableAutoConfiguration注解的关键功能是@Import注解导入的配置功能,EnableAutoConfigurationImportSelector使用SpringFactoriesLoader.loadFactoryNames方法来扫描具有META-INF/spring.factories文件的jar包,此文件中声明了哪些自动配置。
相关文章推荐
- Spring Boot 运行原理之自动配置
- Spring Boot核心原理-自动配置 以及@ConfigurationProperties 注解
- Spring Boot 运行原理 - 查看Spring Boot自动配置项
- SpringBoot核心原理:自动化配置2
- springboot 入门教程(3)-运行原理、关键注解和配置
- SpringBoot学习笔记(3) Spring Boot 运行原理,自动配置
- SpringBoot核心原理:自动化配置1
- Spring Boot核心原理-自动配置
- SpringBoot15-springboot核心-Spring Boot运行原理
- Spring Boot核心原理-自动配置
- Spring Boot 运行原理 - 核心注解
- SpringBoot学习笔记(3) Spring Boot 运行原理,自动配置
- springboot自动配置的核心原理
- Spring Boot核心原理-自动配置
- 20-SpringBoot——核心-运行原理
- Spring Boot核心原理-自动配置
- 【springboot】基于springboot运行原理实现springboot的自动配置
- Spring Boot核心原理-自动配置
- 《Spring Boot 实战:从0到1》第3章 Spring Boot自动配置原理
- Spring Boot核心--基本配置