基本配置:html
1.入口类和@SpringBootApplicationjava
springboot 一般有一个名为*Application 的入口类,入口类里有一个 main 方法,这个 main 方法其实就是一个标准 Java 应用的入口方法。在 main 方法中使用 SpringApplication.run(Application.class, args); ,启动 springboot 应用程序。web
@SpringBootApplication 是 springboot 的核心注解,它是一个组合注解,源码以下:
spring
* Copyright 2012-2017 the original author or authors. package org.springframework.boot.autoconfigure; 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.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.core.annotation.AliasFor; /** * Indicates a {@link Configuration configuration} class that declares one or more * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience * annotation that is equivalent to declaring {@code @Configuration}, * {@code @EnableAutoConfiguration} and {@code @ComponentScan}. * * @author Phillip Webb * @author Stephane Nicoll * @since 1.2.0 */ @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 { /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ @AliasFor(annotation = EnableAutoConfiguration.class) Class<?>[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ @AliasFor(annotation = EnableAutoConfiguration.class) String[] excludeName() default {}; /** * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses} * for a type-safe alternative to String-based package names. * @return base packages to scan * @since 1.3.0 */ @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanBasePackages() default {}; /** * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to * scan for annotated components. The package of each class specified will be scanned. * <p> * Consider creating a special no-op marker class or interface in each package that * serves no purpose other than being referenced by this attribute. * @return base packages to scan * @since 1.3.0 */ @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") Class<?>[] scanBasePackageClasses() default {}; }
@SpringBootApplication 注解主要组合了 @Configuration 、@EnableAutoConfiguration、@ComponentScan;若不使用 @SpringBootApplication 注解则能够在入口类上面直接使用。安全
其中,@EnableAutoConfiguration 让 SpringBoot 根据类路径中 jar 包依赖为当前项目进行自动配置。springboot
例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 springmvc 的依赖,那么 springboot 会对 Tomcat 和 springmvc 进行自动配置。
mvc
又如,添加了 spring-boot-starter-data-jpa 依赖,springboot 会自动进行 jpa 相关的配置。
app
springboot 会自动扫描 @SpringBootApplication 所在类的同级包以及包里的 bean (若为 JPA 项目还能够扫描 @Entity 的实体类)。建议入口类放置的位置在 groupid+arctifactID 组合包下。
框架
2.关闭特定的自动配置eclipse
经过上面的 @SpringBootApplication 的源码咱们能够看出,关闭特定的自动配置应该使用 @SpringBootApplication 注解的 exclude 参数,例如:
@SpringBootApplication(exclude = (xx.class))
3.定制 banner
1).修改 banner
①:在 springboot 启动的时候会有一个默认的启动图案,见上篇文章截图。
②:咱们在 src/main/resources 下新建一个 banner.txt
③:经过 http://patorjk.com/software/taag 网站生成字符,复制到 banner.txt
做者推荐 idea,我的仍是偏心 eclipse,用过 idea 仍是不习惯!!!
4.springboot 的配置文件
springboot 使用一个全局的配置文件 application.properties 或 application.yml ,放置在 src/main/resource 目录或者类路径的 /config 下。
springboot 不单单支持常规的 properties 配置文件,还支持 yaml 语言的配置文件。yaml 是以数据为中心的配置语言,在配置数据的时候具备面向对象的特征。
springboot 的全局配置文件的做用是对一些默认配置的配置值进行修改。
①:简单示例:
将 Tomcat 的默认端口 8080 改成 9090,并将默认的访问路径 / 修改成 /helloboot。
能够在 application.properties 中添加:
application.yml:
从上面的配置能够看出,在 springboot 中,context-path、contextPath 或者 CONTEXT_PATH 形式实际上是通用的。而且,yaml 的配置更加简洁清晰。
5. starter pom
spring boot 为咱们提供了简化企业级开发绝大多数场景的 starter pom, 只要使用了应用场景须要的 starter pom,相关的技术配置将会消除,就能够获得 springboot 为咱们提供的自动配置的 bean。有官方和第三方提供的 starter pom.
6.使用 xml 配置
springboot 提倡零配置,即无 xml 配置,可是实际项目中,可能有一些特殊要求你必须使用 xml 配置,这时咱们能够经过 spring 提供的 @ImportResource 来加载 xml 配置,例如:
@ImportResource({"a.xml","b.xml"})
外部配置
springboot 容许使用 properties 文件、yaml 文件或者命令行参数做为外部配置。
1. 命令行参数配置
springboot 能够是基于 jar 包运行的,打成 jar 包的程序能够直接经过下面命令运行:
java -jar xx.jar
能够经过一下命令修改 Tomcat 端口号
java -jar xx.jar --server.port=9090
2.常规属性配置
在 spring 环境下面,注入 properties 配置文件,只须要经过注解 @PropertySource 指明 properties 文件的位置,而后经过 @Value 注入值。在 springboot 里,只需在 application.properties 里定义属性,而后直接使用 @Value 便可。
3.类型安全的配置(基于 properties)
上例中使用@Value 注入每一个配置在实际项目中会显得格外麻烦,由于咱们的配置一般会有许多个,若使用上例中的方式则要使用@Value 注入不少次。
springboot 还提供了基于类型安全的配置方式,经过 @ConfigurationProperties 将 properties 属性和一个 bean 及其属性关联,从而实现类型的安全的配置。
以前,按照做者的书一路看过来,碰到了第一个错,springboot 会扫描全部的(或者同包下,猜想) Application 文件,另一个 @RequestMapping 也使用了 / ,本觉得我启动 SettingApplication ,Application 不会有影响!小记。
日志配置
springboot 支持 Java Util Logging、Log4J、Log4J2 和 Logback 做为日志框架,不管使用哪一种日志框架,springboot 已为当前使用日志框架的控制台输出以及文件输出作好了配置。
Profile 配置
Profile 是 spring 用来针对不一样的环境对不一样的配置提供支持,全局 Profile 配置使用 application-{profile}.properties ( 如 application-prod.properties)。
经过 application.properties 中设置 spring.profiles.active= prod 来指定活动的 Profile。
下面咱们作一个示例,咱们分为生产环境和开发环境,生产环境端口号为 80, 开发环境为 8888。