SpringBoot的配置详解
SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties
或者application.yml
(application.yaml
)进行配置。
SpringBoot默认会从Resources目录下加载application.properties
或application.yml(application.yaml)
文件
一、application.yml
YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数 据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导 入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以数据为核心的,比传统的xml方式更加简洁。 YML文件的扩展名可以使用.yml或者.yaml
1.配置普通数据
语法: key: value
name: tracy
注意:value之前有一个空格
2.配置对象数据
语法:
key:
key1: value1
key2: value2
或者:
key: {key1: value1,key2: value2}
示例:
#对象数据
user:
name: tracy
age: 25
address: shenzhen
#行内
user: {name: tracy,age: 25,address: shenzhen}
3.配置Map数据(与对象数据配置相同)
key:
key1: value1
key2: value2
#Map(与对象配置相同)
userMap:
name: hcx
age: 24
4.配置集合数据
语法:
key:
- value1
- value2
或者:
key: [value1,value2]
示例:
#配置普通字符串集合
city:
- beijing
- shanghai
- guangzhou
- shenzhen
city: [beijing,shanghai,guangzhou,shenzhen]
#配置对象集合
student:
- name: tracy
age: 25
address: shenzhen
- name: hcx
age: 24
address: gaungzhou
student: [{name: tracy,age: 25,address: shenzhen},{name: hcx,age: 24,address: gaungzhou}]
二、 配置文件与配置类的属性映射方式
1.使用@Value
注解映射
@Value
注解将配置文件中的值映射到一个Spring管理的Bean的字段上
package com.hcx.springbootdemo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by hongcaixia on 2020/2/10.
*/
@Controller
public class ConfigController {
@Value("${name}")
private String name;
@Value("${user.address}")
private String address;
@RequestMapping("/config")
@ResponseBody
public String config(){
System.out.println(name);
return "hello,"+name+","+address;
}
}
2.使用@ConfigurationProperties
注解映射
通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")
可以将配置文件中的配置自动与实体进行映射
添加执行器依赖:(配置了可以在写配置文件时有提示)
<!--@ConfigurationProperties执行器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
package com.hcx.springbootdemo.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by hongcaixia on 2020/2/10.
*/
@Controller
@ConfigurationProperties(prefix = "worker")//自动从配置文件中找出worker开头的
public class ConfigController {
private String name;
private String address;
private String age;
@RequestMapping("/config")
@ResponseBody
public String config(){
System.out.println(name);
return name+age+address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
注意:使用
@ConfigurationProperties
方式可以进行配置文件与实体字段的自动映射,字段必须提供set方法才可以,而使用@Value
注解修饰的字段不需要提供set方法
demo:
JdbcProperties:
package com.hcx.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by hongcaixia on 2020/2/11.
*/
@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
方式一:通过注入:
package com.hcx.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* Created by hongcaixia on 2020/2/10.
*/
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
//通过注入的方式
@Autowired
private JdbcProperties jdbcProperties;
@Bean //把方法的返回值注入到spring容器
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.jdbcProperties.getDriverClassName());
dataSource.setUrl(this.jdbcProperties.getUrl());
dataSource.setUsername(this.jdbcProperties.getUsername());
dataSource.setPassword(this.jdbcProperties.getPassword());
return dataSource;
}
}
方式二:通过方法形参:
package com.hcx.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* Created by hongcaixia on 2020/2/10.
*/
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
/**
* 通过方法形参传递
* @param jdbcProperties 形参
* @return
*/
@Bean //把方法的返回值注入到spring容器
public DataSource dataSource(JdbcProperties jdbcProperties){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
}
}
方式三:把注解@ConfigurationProperties
作用于方法上
package com.hcx.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* Created by hongcaixia on 2020/2/10.
*/
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Bean //把方法的返回值注入到spring容器
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}
三、静态资源文件位置
访问路径:ip地址:端口号/项目根路径/静态资源文件名
四、拦截器
1.自定义拦截器:实现HandlerInterceptor接口
MyInterceptor:
package com.hcx.interceptor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 定义拦截器
* Created by hongcaixia on 2020/2/11.
*/
@Component
public class MyInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("前置方法执行了");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
System.out.println("后置方法执行了");
}
}
2.配置拦截器,自定义一个Java配置类(@Configuration),实现WebMvcConfigurer接口。
WebMvcConfiguration:
package com.hcx.config;
import com.hcx.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 配置拦截器
* Created by hongcaixia on 2020/2/11.
*/
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer{
@Autowired
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor).addPathPatterns("/**");
}
}
转载自:https://juejin.cn/post/7248906639705522237