参考:SpringBoot学习:https://how2j.cn/k/springboot/springboot-eclipse/1640.html?p=36286
一、注解介绍
@EnableAutoConfiguration:
让SpringBoot根据类路径中的jar包依赖为当前项目进行自动配置。
spring-boot-starter-web:
SpringBoot会自动对Tomcat和SpringMVC进行自动配置。
exclude参数:关闭特定的自动配置。
例:@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
application.properties或application.yml:
放置在src/main/resources目录下。
SpringBoot的全局配置文件的作用:是对一些默认配置的配置项进行修改。
修改端口号和默认访问地址
server.port=80
server.context-path=/hello
使用xml配置
实际项目中,难免有一些特殊要求会使用到xml配置,我们可以使用Spring提供的@ImportResource来加载xml配置
@ImportResource("classpath:some-context.xml","classpath:another-context.xml")
二、常规属性配置
注入properties文件里的值的方式
在Spring中:通过@PropertySource指明properties文件的位置,然后使用注解@Value注入值。
在SpringBoot中:只需要在application.properties定义属性,直接使用@Value注入即可。
例:
(1)在application.properties里面增加属性:
blog.author=eleven
blog.age=21
(2)修改入口类:
package com.eleven.springboot02;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Springboot02Application {
@Value("${blog.author}")
private String blogAuthor;
@Value("${blog.age}")
private Long blogAge;
@RequestMapping("/")
String index(){
return "book name is: "+blogAuthor +" and blog age is: "+blogAge;
}
public static void main(String[] args) {
SpringApplication.run(Springboot02Application.class, args);
}
}
(3)运行访问
http://localhost:8080/
三、类型安全的配置
上例中的@Value注入,因为我们的配置通常会是很许多个,若是使用@Value的方式则需要注入很多次。
基于类型安全的配置方式,@ConfigurationProperties将properties属性和一个bean及属性关联,从而实现类型安全的配置。
(1)继续在上例中的代码进行修改
新建一个pojo包,在包下面新建AuthorSettings类
package com.eleven.springboot02.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "blog") // 通过该注解加载properties里面的配置,通过prefix指定properties配置的前缀
public class AuthorSettings {
private String author;
private Long age;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
(2)在入口类进行修改
package com.eleven.springboot02;
import com.eleven.springboot02.pojo.AuthorSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Springboot02Application {
@Autowired
private AuthorSettings authorSettings; // 将该类注入到当前类中
@RequestMapping("/")
String index(){
return "book name is: "+authorSettings.getAuthor() +" and blog age is: "+authorSettings.getAge();
}
public static void main(String[] args) {
SpringApplication.run(Springboot02Application.class, args);
}
}
(3)运行访问
四、日志配置
SpringBoot默认使用的日志框架是:Logback
(1)配置日志文件
logging.file=D:/mylog/log.log
(2)配置日志级别
logging.level.org.springframework.web=DEBUG
五、Profile配置
Profile使用来针对不同的环境对不同的配置提供支持的。
比如:开发环境是dev,端口号是80;测试环境是test,端口号是8888
(1)新建2个properties
开发:application-dev.properties
server.port=80
测试:application-test.properties
server.port=8888
在application.properties中加入
spring.profiles.active=dev