基本配置:
1.入口类和@SpringBootApplication
SpringApplication.run(Application.class, args); ,启动 springboot 应用程序。
@SpringBootApplication 是 springboot 的核心注解,它是一个组合注解,源码如下:
* 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 包依赖为当前项目进行自动配置。
例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 springmvc 的依赖,那么 springboot 会对 Tomcat 和 springmvc 进行自动配置。
又如,添加了 spring-boot-starter-data-jpa 依赖,springboot 会自动进行 jpa 相关的配置。
springboot 会自动扫描 @SpringBootApplication 所在类的同级包以及包里的 bean (若为 JPA 项目还可以扫描 @Entity 的实体类)。建议入口类放置的位置在 groupid+arctifactID 组合包下。
2.关闭特定的自动配置
@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。