Spring boot

SpringBoot 注解之@Configuration, @C

2021-02-23  本文已影响0人  三也视界

@Configuration这个注解的主要作用是把使用该注解的类当做Bean来使用,用于配置Spring容器。
@ConfigurationProperties(prefix="xxxx") 用来获取相同父标签的元素的值。具体使用案例见下文。
@EnableConfigurationProperties(xxx.class)将配置好的类进行使用,在内填入参数类。

yaml文件上自定义字段,对应ConfigurationProperties上的prefix,至于yaml文件的一些语法,请参考文章SpringBoot配置文件.yaml(.yml)和.properties

#jwt
jwt:
  header: Authorization
  # 令牌前缀
  token-start-with: Bearer
  secret: k09BQnaF
  # 必须使用最少88位的Base64对该令牌进行编码
  base64-secret: ZmQ0ZGI5NjQ0MDQwY2I4MjMxY2Y3ZmI3MjdhN2ZmMjNhODViOTg1ZGE0NTBjMGM4NDA5NzYxMjdjOWMwYWRmZTBlZjlhNGY3ZTg4Y2U3YTE1ODVkZDU5Y2Y3OGYwZWE1NzUzNWQ2YjFjZDc0NGMxZWU2MmQ3MjY1NzJmNTE0MzI=
  # 令牌过期时间 此处单位/毫秒 ,默认4小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
  token-validity-in-seconds: 14400000
  # 在线用户key
  online-key: online-token
  # 验证码
  code-key: code-key

具体的配置类,对应的字段自动将yaml文件上的-按照驼峰转换,@ConfigurationProperties需要和@Configuration配合使用

@Data
@Configuration
@ConfigurationProperties(prefix = "jwt")
public class SecurityProperties {

    /** Request Headers : Authorization */
    private String header;

    /** 令牌前缀,最后留个空格 Bearer */
    private String tokenStartWith;

    /** 必须使用最少88位的Base64对该令牌进行编码 */
    private String base64Secret;
    private String secret;
    /** 令牌过期时间 此处单位/毫秒 */
    private Long tokenValidityInSeconds;

    /** 在线用户 key,根据 key 查询 redis 中在线用户的数据 */
    private String onlineKey;

    /** 验证码 key */
    private String codeKey;

    public String getTokenStartWith() {
        return tokenStartWith + " ";
    }
}

上面的例子将会读取properties或yaml文件中所有以mail开头的属性,并和bean中的字段进行匹配

如果你不想使用@Configuration, 那么需要在@EnableConfigurationProperties注解中手动导入配置文件如下:

@SpringBootApplication
@EnableConfigurationProperties(ConfigProperties.class)
public class ConfigPropApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigPropApp.class,args);
    }
}

我们也可以在@ConfigurationPropertiesScan中指定Config文件的路径:

@SpringBootApplication
@ConfigurationPropertiesScan("com.example.config")
public class ConfigPropApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigPropApp.class,args);
    }
}

这样的话程序只会在com.example.config包中查找config文件。

如果项目配置了多数据源,那么启动的时候会报循环引用的问题

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   servletEndpointRegistrar defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration.class]
      ↓
   healthEndpoint defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]
      ↓
   org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration
┌─────┐
|  dataSource
↑     ↓
|  scopedTarget.dataSource defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]
↑     ↓
|  org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker
└─────┘

两个办法避免这个错误:

1, 在application.properties中加入spring.datasource.initialize=false
2,在SpringBootApplication下面添加 @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
参考 https://github.com/heikehuan/springboot-multiple-dataSources/issues/2

添加@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) 解决问题

https://github.com/spring-projects/spring-boot/issues/13406
这个更改spring-boot-starter-parent的版本并不能解决问题,我的是2.4.2还是出现同样的问题。

上一篇下一篇

猜你喜欢

热点阅读