springboot 第一篇 (核心)

版权声明:如果觉得好的话,不防点个赞,那点你们认为不对或是需要补充,可以留言啊!本人原创,未经允许不得转载!! https://blog.csdn.net/qq_28289405/article/details/84551284

1.1基本配置

1.1.1 入口类和@SpringBootApplication

@SpringBootApplication 注解组合了@EnableAutoConfiguration 、@ComponentScan、@Configuration; 若不是用@SpringBootApplication 注解,则可以在入口类上直接使用 @EnableAutoConfiguration 、@ComponentScan 和 @Configuration 。

其中, @EnableAutoConfiguration 让 Spring Boot 根据类路径中的jar包依赖为当前项目进行自动配置。

Spring Boot 会自动扫描 @SpringBootApplication 所在类的同级包,以及下级里的 Bean (若为 JPA 项目还可以扫描标注 @Entity 的实体类)。建议入口类放置的位置在 groupId + arctifactId 组合的包名下。

1.1.2 关闭特定的自动配置

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

1.1.3 定制 Banner

1、修改 Banner 

在src/main/resources 下创建banner.txt , 在 http://patorjk.com/software/taag 网站生成字符,粘贴复制到banner.txt 中。

2、关闭 banner

(1)main 里的内容修改为:

SpringApplication app = new SpringApplication(GatewayApplication.class);
app.setShowBanner(false);
app.run(args);

(2)或使用fluentAPI修改为:

new SpringApplicationBuilder(GatewayApplication.class).showBanner(false).run(args);

1.1.4 Spring Boot 的配置文件

Spring Boot 使用一个全局的配置文件 application.properties 或是 application.yml ,放置在 src/main/resources 目录或者类路径的 /config 下。

1.1.5 使用 xml 配置

Spring Boot 提倡零配置 ,即无 xml 配置,但是在实际项目中,可能有一些特殊要求你必须使用xml 配置,这时我们可以通过 Spring 提供的 @ImportResource 来加载 xml 配置。

@ImportResource({"calsspath:some-context.xml","classpath:another-context.xml"})

1.2外部配置

Spring Boot 允许使用 properties 文件、yaml 文件或者命令行参数作为外部配置。

6.2.1 命令行参数配置

打成jar包的命令:

java -jar xxx.jar

修改端口号:

java -jar xxx.jar --server.port=9090

1.2.2 常规属性配置

在 Spring Boot 里,我们只需要在application.properties 定义属性,直接使用 @Value  注入即可。

(1)在  application.properties 定义属性 

book.author = xxx;
book.name = yyy;

(2) 修改入口类

@SpringBootApplication
public class AuthServerApplication {

	@Value("${book.author}")
	private String bookAuthor;
	
	@Value("{book.name}")
	private String bookName;
	
	@RequestMapping("/")
	String.index(){
		return "book name is:" + bookName + "and book author:" + bookAuthor;
	}
	
	public static void main(String[] args) {
		SpringApplication.run(AuthServerApplication.class, args);
	}
}

1.2.3 类型安全的配置(基于properties)

通过 @ConfigurationProperties 将 properties 属性和一个Bean 以及属性关联,从而实现类型安全的配置。

(1)、在 application.properties 上添加

author.name = ccc
anthor.age = 22

(2) 、在Bean 类中

@Compent
@ConfigurationProperties(prefix = "author")
public class Bean {

    private String name;
    private Integer age ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

代码解释:

通过 @ConfigurationProperties 加载 properties 文件内的配置,通过 prefix 属性指定 properties 的配置的前缀,通过 locations 指定 properties 文件的位置。 

例如 @ConfigurationProperties(prefix = "author" ,locations = "classpath:config/author.properties")

1.2.4 日志配置

Spring Boot 支持 Java Util Logging 、Log4j 、Log4J2 和 Logback 作为日志框架,无论使用哪种日志框架,Spring Boot已为当前使用日志框架的控制台和控制台的输出以及文件输出做好了配置。

Spring Boot 默认是 使用 Logback  作为日志框架。

日志级别从低到高分为:
TRACE < DEBUG < INFO < WARN < ERROR < FATAL 。

配置 logging.file ,会在项目的当前路径下生成一个 xxx.log 日志文件。

logging.file = D:/log/log.log
所有支持的日志记录系统都可以在Spring环境中设置记录级别(例如在application.properties中) 
格式为:'logging.level.* = LEVEL'

logging.level:日志级别控制前缀,*为包名或Logger名 
LEVEL:选项TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF

1.2.5 Profile 配置

Profile  是Spring 用来针对不同的环境对不同的配置提供支持的,全局 Profile  配置使用 application-{ profile }.properties 

可参考:SpringBoot之开启Profile

1.3 Spring Boot 运行原理

通过以下方式来查看当前项目中已启动和未启动的自动配置的报告。

(1)、运行jar 时增加 --debug 参数:

java -jar xx.jar --debug

(2)、在 application.properties 中设置属性:

debug = true

1.3.1 运作原理

关于 Spring Boot 的运作原理 ,@SpringBootApplication 这个注解是一个组合注解,它的核心功能是由  @EnableAutoConfiguration  注解提供的。

@EnableAutoConfiguration 注解的源码:

这里的关键功能是 @Import 注解导入的配置功能 ,AutoConfigurationImportSelector  使用 SpringFactoriesLoader.loadFactoryNames 方法来描述具有 META-INF/spring.factories 文件的jar包。

1.3.2 核心注解

在   下

这些注解组成了 @Conditional 注解 , 只是使用了不同的条件(Condition)。

猜你喜欢

转载自blog.csdn.net/qq_28289405/article/details/84551284