Spring Boot优化了很多配置的方式方法,让我们少去了很多配置的环节。但又给了我们自定义配置的选择权。下面将详细说明Spring Boot覆盖自动配置原理、SpringBoot配置Web端口号、数据库连接属性、自定义log4j配置、application.properties自定义键值对、Profile对于生产环境和开发环境的不同配置。

1. 覆盖Spring Boot自动配置

我们可以自定义配置来覆盖原来的Spring Boot的自动配置,这是由于在原来的配置的类,注解了如@ConditianalOnMissingBean,当源码中有这个注解,在我们存在JdbcOperations.class类时,我们的JdbcOperations.class将会覆盖Spring Boot的自动配置。



当我们导入spring-boot-starter-security时,我们要新建一个SecurityConfig类,并继承WebSecurityConfigurerAdapter, 就可以覆盖Spring Boot自动配置的Security模块(原来是Spring security 是一打开网页就让我们登陆,这显然是需要我们进行自定义配置的),只需要复写WebSecurityConfigurerAdapter的configure(HttpSecurity http)方法和configure(AuthenticationManagerBuilder auth)方法,就可以对用户身份的验证。

configure(HttpSecurity http):可设置访问url, 允许登陆的条件,设置登陆表单路径,登陆错误的rul.

configure(AuthenticationManagerBuilder auth): 可获取登陆用户的信息。

SpringBoot自动配置Security类:


@ConditionalOnMissBean(WebSecurityConfiguration.class)表示,在我们的程序中无WebSecurityConfiguration.class时,这个SpringBoot的内置配置才生效。

@ConditonalOnClass({EnableWebSecurity.class})表示,我们程序中自定义的SecurityConfig.class也需要像这个类一样,注解EnableWebSecurity,所以在SecurityConfig类上需要注解@EnableWebSecurity

2. Spring Boot轻松配置端口号,数据库连接池

只需在Application.properties中配置SpringBoot规定Key,SpringBoot将自动配置数据库端口号等属性。

#指定服务器端口号
server.port=80

#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassname=com.mysql.jdbc.Driver

#数据库连接池
spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

但是这些key值不能随意更改,只能官方指定的key值,才能配置相应的属性。包括spring.view.prefix,视图前缀。spring.view.suffix 视图后缀。


3. 自定义log4j配置文件。

3.1 在spring-boot-starter中排除掉内置的日志依赖

<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> 

3.2 加入log4j依赖,不用添加verision, Spring Boot Starter会为我们找到合适的version. 如果spring boot 没有为我们找到合适的version, Maven会报错,在从Maven中央库中找到一个version即可。

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-log4j</artifactId> 
</dependency> 

3.3 指定log4j.properties文件

logging.config.classpath=log4j.properties


4. application.properties自定义配置键值对

    如果需要自定义配置key=value。可自定义一个Bean类,在application.properties文件中,写入key=value,通过Bean类的get方法得到value。

4.1 定义一个Bean类,此类可方便集中管理,方便后期维护。@ConfigurationProperties("myConfigKey"),将扫描application.properties文件,获得value值。

package com.plin.myconfig;  
 
import org.springframework.boot.context.propertiezs. 
                                   ConfigurationProperties; 
import org.springframework.stereotype.Component; 
 
@Component 
@ConfigurationProperties("myConfigKey")  
public class MyConfigProperties { 
  private String myConfigValue; 
 
  public void setMyConfigValue(String myConfigValue) {  
    this.myConfigValue= myConfigValue; 
  } 
 
  public String getMyConfigValue() { 
    return myConfigValue; 
  } 
} 

4.2 appliction.properties中添加myConfigKey=myConfigValue;

myConfigKey=myConfigValue

4.3 使用自定义配置 ,用get方法得到自定义配置的value。

@RestController
@RequestMapping("/")  
public class TestController { 
      @AutoWired
      private MyConfigProperties myConfigProperties; 
    @RequestingMapping(/test)
    public void test(){
        String value = myConfigProperties.getMyConfigValue();
        System.out.println(value);
    }
}

5. 使用Profile进行条件化配置

    使用Profile可以配置开发环境属性和生产环节属性。比如数据库连接池属性。

5.1  激活prodution Profile,需要激活,才能应用Profile配置

    在application.properties中配置

spring.profiles.active=production

5.2  在类或方法上注解 @Profile("production") ,此类或方法将在生产环境中生效。开发环境无需配置,默认极为开发环境。

5.3  通过application-{profile}.properties.

    开发环境配置文件application-development.properties,配置日志级别为DEBUG.

logging.level.root=DEBUG
    生产环境配置文件application-production.properties将会在生产环境生效。配置日志级别为WARN,并配置将日志写入配置文件。
logging.path=/var/logs/ 
logging.file=production.log 
logging.level.root=WARN 
5.4  一些不需要特别指明运行环境的配置,同样可以配置在application.properties中。

更多推荐

Spring Boot自动配置和自定义配置属性详解