一、@SpringBootApplication

@SpringBootApplication是spring boot的核心注解,源码如下:

相当于:@Configuration+@EnableAutoConfiguration+@ComponentScan

@Configuration:此类是一个配置

@EnableAutoConfiguration:让springboot根据类路径中的jar包依赖为当前项目进行自动配置

@ComponentScan:springboot自动扫描入口类所在包以及其子包里的bean

另外springboot也可以关闭特定的自动配置:@SpringBootApplication(exlude = {DataSourceAutoConfiguration.class})

二、SpringBoot配置文件

1、application.properties或application.yml

SpringBoot的全局配置文件为applicaiton.properties或者application.yml,通常放在src/main/resources/下,如下:

同样也可以使用yml编写,示例如下(将tomcat默认端口修改为8001、将默认访问路径修改为/index):

A、application.properties

server.port=8001

server.context-path=/index

B、application.yml

server:

  port:8001

  context-path: /index

2、加载xml

springboot提倡零配置,但是项目中难免需要使用xml配置,引入方式如下:

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

三、外部配置

1、常规属性配置

在spring的项目中,通过@PropertySource指明properties文件的位置,然后可以通过@Value注入值。在SpringBoot里,只需要在appllication.properties文件中定义属性,直接使用@Value即可

示例:

application.properties文件增加属性:

运行结果:

注意点:

A、@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})  ----------------------->项目启动的时候会报错,提示没有数据源,因此在初始化bean的时候,排除这个bean

B、项目运行地址是不需要项目名的,直接使用http://localhost:8080/index,而不是http://localhost:8080/项目名称/index

2、类型安全的配置

通过@Value的方式配置属性也可以,但是当属性过多时,会写许多个@Value,同时也不美观,因此springboot还提供了@ConfigurationProperties将属性与一个bean关联,示例如下:

运行结果:

注意点:

A、使用lombok的@Data可以省去写get、set方法

B、有时候会提示一个错误:Spring Boot Annotion processor not found in classpath

这时需要添加一个依赖,如下:

C、在加载application.properties中的属性时,不需要指定location,如果是在其他自定义的properties文件中去加载属性,则需要加上location(1.5.1以上版本取消)

@ConfigurationProperties(prefix = "xxx", locations = {"classpath:/xxx.properties"})

如果springboot版本高于1.5.1,则可以使用这种方式(将类注册为@component,并且使用@ConfigurationProperties+@PropertySource):

四、日志配置

springboot支持log4j、logback等等作为日志框架,但是默认日志框架使用的是logback,示例如下:

配置日志文件:

logging.file=D:/mylog/log.log

配置日志级别:

logging.level.org.springframework.web=DEBUG

springboot深入学习(一)-----springboot核心、配置文件加载、日志配置的更多相关文章

  1. Selenium2学习(十四)-- 加载Firefox配置

    前言有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile_ ...

  2. Nginx 重新加载日志配置

    最近在写一个nginx日志的切割脚本,切割完后,发现可以不重启服务,而直接重新加载日志配置文件的命令 [   kill -USR1 $nginx.pid   ],但是不知道 -USR1这个参数是什么意 ...

  3. springboot启动流程(四)application配置文件加载过程

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 触发监听器加载配置文件 在上一篇文章中,我们看到了Environment对象的创建方法.同时也 ...

  4. Springboot学习04-默认错误页面加载机制源码分析

    Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...

  5. springboot的yaml基础语法与取值,配置类,配置文件加载优先级

    1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...

  6. SpringBoot系列之配置文件加载位置

    SpringBoot系列之配置文件加载位置 SpringBoot启动会自动扫描如下位置的application.properties或者application.yml文件作为Springboot的默认 ...

  7. SpringBoot——配置文件加载位置及外部配置加载顺序

    声明 本文部分转自:SpringBoot配置文件加载位置与优先级 正文 1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者applic ...

  8. springboot错误: 找不到或无法加载主类

    一:当在eclipse启动spring boot项目时出现问题: springboot错误: 找不到或无法加载主类 解决办法: 1,通过cmd命令行,进入项目目录进行,mvn clean instal ...

  9. Spring Boot 2.4.0正式发布,全新的配置文件加载机制(不向下兼容)

    千里之行,始于足下.关注公众号[BAT的乌托邦],有Spring技术栈.MyBatis.JVM.中间件等小而美的原创专栏供以免费学习.分享.成长,拒绝浅尝辄止.本文已被 https://www.you ...

  10. asp.netcore 深入了解配置文件加载过程

    前言     配置文件中程序运行中,担当着不可或缺的角色:通常情况下,使用 visual studio 进行创建项目过程中,项目配置文件会自动生成在项目根目录下,如 appsettings.json, ...

随机推荐

  1. Codeforces Beta Round #52 (Div. 2)

    Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...

  2. C++ 求最长递增子序列(动态规划)

    i 0 1 2 3 4 5 6 7 8 a[i] 1 4 7 2 5 8 3 6 9 lis[i] 1 2 3 2 3 4 3 4 5 时间复杂度为n^2的算法: //求最长递增子序列 //2019/ ...

  3. tiny4412SDK 1312B 启动ubuntuDsektop

    1,解压光盘所带文件ubuntu-desktop-sdcard-image-YYYYMMDD.tar.gz , 得到ubuntudesktop-8g.raw 2,先用SD-flash刷写一边B盘ima ...

  4. System.ServiceProcess.TimeoutException: Time out has expired and the operation has not been completed.

    项目代码如下 ServiceController service = new ServiceController("ModbusAgent"); service.Stop(); T ...

  5. Python ctypes.windll.user32() Examples

    Example 1 Project: OSPTF   Author: xSploited   File: mouselogger.py View Source Project 7 votes def ...

  6. angular 1.x 控制器之间互相传递参数

    我们要向前方看齐,基于js引用类型的对象就不记了,所以使用基于事件的方式: angular 中 $on,$emit,$boardcast来实现父控制器和子控制器互相通讯, 其中$on表示事件监听, $ ...

  7. c#程序设计原则

    单一职责 开闭原则:对扩展开放,对修改封闭. 方法 的职责,一个方法做的事越多,造成问题的可能性会增加. 解决的方法1:就是分拆2:写单独类

  8. hdu 1175(BFS&DFS) 连连看

    题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...

  9. Startup.国外新锐公司及其技术Blog

    国外技术公司Tech/Engineering Blog 1. vimeo https://coderwall.com/team/vimeo http://blog.assembly.com/ 2. l ...

  10. How do I configure a Wired Ethernet interface

    1.In order to configure the Wired Ethernet interface the MDI must be connected to the PC using the U ...