定义:
习惯优于配置,spring boot可以不用或者只需要很少的Spring配置
核心功能:
1、独立运行的Spring项目
2、内嵌Servlet容器
3、提供stater简化Maven配置
4、自动配置Spring
5、准生产的应用监控
6、无代码生成和xml配置
优点:
(1)快速构建项目
(2)对主流开发框架的无配置集成
(3)项目可独立运行,无须外部依赖Servlet容器
(4)提供运行时应用监控
(5)极大地提高了开发、部署效率
(6)与云计算的天然集成
一、Spring Boot核心
1、基本配置 |-- 1.1 入口类和@SpringBootApplication |-- @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Configuration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplication { Class<?> exclude() default {}; String[] excludeName() default {}; } |-- 1.2 关闭特定的 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) |-- 1.3 定制banner |-- src |-- main |-- resources |-- banner.txt spring字样 |-- 开户banner 替换即可 |-- 关闭banner main { app = new SpringApplication(Ch522Application.class); app.setShowBanner(false); app.run(args); // 或者 new SpringApplicationBuilder(Ch522Application.class) .showBanner(false) .run(args); } |-- 1.4 Spring boot 的配置文件 |-- application.properties server.port=9090 server.context-path=/helloboot |-- application.yml server: port: 9090 context-path: helloboot |-- 1.5 使用xml配置 @ImportSource({"classpath:some-context.xml", "classpath:another-context.xml"}) 2、外部配置 2.1 命令行参数: java -jar xx.jar --server.port=9090 2.2 常规属性配置: |-- application.properties book.author=zhangsan book.name=springboot |-- @RestController @SpringBootApplication Ch522Application { @Value("book.author") private String bookAuthor; @Value("book.name") private String bookName; @RequestMapping("/") String index() { return "book name is:" + bookName + " and book author is:" + bookAuthor; } public static void main(String[] args) { SpringApplication.run(Ch522Application.class, args); } } 2.3 类型安全配置(基于properties) |-- application.properties author.name=wyf author.age=32 |-- @Component @ConfigurationProperties(prefix = "author") public class AuthorSettings { private String name; private Long age; // setter、getter } 2.4 Profile配置 |-- application-prod.properties server.port=80 |-- application-dev.properties server.port=8888 |-- application.properties spring.profiles.active=dev 2.5 spring boot运行原理 开启debug模式: (1)运行jar时增加--debug参数 java -jar xx.jar --debug (2)在application.properties中设置属性 debug=true (3)在STS中设置 VM arguments -Ddebug 2.5.1 运作原理: |-- @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({EnableAutoConfigurationImportSelector.class, AutoConfigurationPackages.Registrar.class }) public @interface EnableAutoConfiguration { Class<?> exclude() default {}; String[] excludeName() default {}; } |-- EnableAutoConfigurationImportSelector -> 使用SpringFactoriesLoader.loadFactoryNames方法扫描具有META-INF/spring.factories的jar -> 而spring-boot-autoconfigure-1.3.0.x.jar里就有一个spring.factories,此文件申明了一些自动配置 2.5.2 核心注解 打开spring.factories文件下的xxAutoConfiguration文件 |-- @ConditionalOnBean:当容器有指定的Bean条件下 |-- @ConditionalOnClass:当类路径下有指定的类的条件下 |-- @ConditionalOnExpression:基于SpEL表达式作为判断条件 |-- @ConditionalOnWebApplication:当前项目是Web项目的条件下 ... |-- @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional public @interface ConditionalOnWebApplication { } 3. Spring Web 1、静态首页的支持: classpath:/META-INF/resources/index.html classpath:/resources/index.html classpath:/static/index.html classpath:/public/index.html 2、接管Springboot的web配置 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegitry registry) { // 与springboot自动配置同时生效 registry.addViewController("/xx").setViewName("/xx"); } } 3、注册Servlet、Filter、Listener (1)直接注册Bean示例 @Bean public XxServlet xxServlet() { return new XxServlet(); } @Bean public YyFilter yyFilter() { return new YyFilter(); } @Bean public ZzListener zzListener() { return new ZzListener(); } (2)通过RegistrationBean @Bean public ServletRegistrationBean xxServlet() { return new ServletRegistrationBean(new XxServlet(), "/xx/*"); } @Bean public FilterRegistrationBean xxServlet() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new YyFilter()); registrationBean.setOrder(2); return registrationBean; } @Bean public ServletListenerRegistrationBean xxServlet() { return new ServletListenerRegistrationBean<ZzListener>(new ZzListener()); } 待续。。。