设置启动图标
在resources路径下添加自定义的banner.txt文件
自定义图标生成的网址 patorjk.com/software/ta…
取消启动的图标
@SpringBootApplication
public class SpringbootDemo04Application {
public static void main(String[] args) {
//SpringApplication.run(SpringbootDemo04Application.class, args);
SpringApplication sa = new SpringApplication(SpringbootDemo04Application.class);
sa.setBannerMode(Banner.Mode.OFF);
sa.run(args);
}
}
配置文件
springboot一般有两种配置文件
applicationContext.properties, applicationContext.yml
修改tomcat配置
server.port=8082
server.servlet.context-path=/springboot
在属性文件中修改编码
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
spring.messages.encoding=UTF-8
IDEA开发工具的编码设置
在属性文件中自定义属性
#自定义属性
user.username=brucehao
user.age=18
user.address=浙江宁波
在控制器中获取自定义属性
@RestController
public class TestController {
@Value("${user.username}")
private String userName;
@Value("${user.age}")
private int age;
@Value("${user.address}")
private String address;
@GetMapping("/hello")
public String test() {
System.out.println("hello world!....");
return "hello world! test...." + userName + " " + age + " " + address;
}
}
启动访问路径 http://localhost:8082/springboot/hello
类型安全配置
使用一个实体类与属性文件中的属性进行映射匹配 先要导入@ConfigurationProperties注解的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
然后设置一个实体类,@ConfigurationProperties注解的属性prefix与配置文件中一致
applicationContext.properties
#自定义属性
user.username=brucehao
user.age=18
user.address=浙江宁波
实体类
//属性文件中的属性和User对象中的成员变量映射
@ConfigurationProperties(prefix = "user")
@Component
public class User {
private String username;
private Integer age;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"username='" + username + ''' +
", age=" + age +
", address='" + address + ''' +
'}';
}
}
在控制器中通过@Autowired注解或者@Resource注解来获取User对象
Logback日志
日志级别从低到高分为:
TRACE < DEBUG < INFO < WARN < ERROR < FATAL
如果设置为 WARN ,则低于 WARN 的信息都不会输出。
Spring Boot 中默认配置 ERROR 、 WARN 和 INFO 级别的日志输出到控制台。
在属性文件中添加
#log配置
logging.file.path=e:/log
logging.level.org.springframework.web=DEBUG
或者在resources路径下配置logback.xml文件
在创建Spring Boot工程时,我们引入了 spring-boot-starter ,其中包含了 spring-boot-starter-logging,该依赖内容就是Spring Boot默认的日志框架Logback,所以我们在引入log4j之前,需要先排除该包的依赖,再引用log4j的依赖。 SpringBoot内置的有Logback的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
在src/main/resources目录下加入log4j.properties配置文件
logging.level :日志级别控制前缀,*为包名或Logger名 例:logging.level.com.test=DEBUG:com.test包下所有class以DEBUG级别输出
生产与开发环境(profiles)的属性文件切换
application.properties文件中配置:
spring.profiles.active=dev ?prod
静态资源处理
项目默认:static目录下存放静态文件,templates下存放动态文件
路径设置覆盖:
custome:自己在已有的resources目录下创建的路径
webapp:自己创建,再将目录的属性设置为resources
#表示所有的访问都要经过静态资源路径
spring.webflux.static-path-pattern=/**
#覆盖掉默认的配置 自己手动加上静态资源的路径 /META-INF/resouces可能是第三方jar包的静态文件
spring.web.resources.static-locations=classpath:/META-INF/resources,classpath:/resources/,classpath:/static/,classpath:/custome/
springboot的自动装配
@Import注解
SpringBoot交给IOC管理对象的几种方式:
@EnableAutoConfiguration注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
自动装配:
1.在SpringBoot项目启动的时候,会加载SpringBootApplication这个注解
2.会解析@EnableAutoConfiguration注解
3.与之对应的解析@Import注解
4.执行ImportSelector接口的实现里的selectImports(...)方法
5.加载META-INF/spring.factories各种类路径【第三方扩展也同样的会加载对应的文件 SPI扩展机制】
6.加载META-INF/spring-autoconfigure-metadata.properties中的注解元数据信息,有条件地过滤spring.factories文件的类