栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot高级特性

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot高级特性

高级特性 Profile

为了方便多环境开发,springboot简化了profile功能

application-prod.yaml 开发环境配置

server:
  port: 5001

application-test.yaml 测试环境配置

server:
   port: 8080

application.yaml 使用profile

spring:
  profiles:
    active: test或者prod

第二种激活方式, 命令行激活

java -jar xxxx.jar spring.profiles.active=prod/test

2.条件装配
@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {

    // ...

}
3.profile分组
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq

使用:--spring.profiles.active=production  激活
配置加载优先级

将一些所需要的信息(数据库账号密码等)抽取出来放在一个文件中,放在外面集中管理。就称为外部化配置

  1. Default properties (specified by setting SpringApplication.setDefaultProperties).
  2. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
  3. Config data (such as application.properties files)
  4. A RandomValuePropertySource that has properties only in random.*.
  5. OS environment variables.
  6. Java System properties (System.getProperties()).
  7. JNDI attributes from java:comp/env.
  8. ServletContext init parameters.
  9. ServletConfig init parameters.
  10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  11. Command line arguments.
  12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  13. @TestPropertySource annotations on your tests.
  14. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.
外部配置源

Java文件、Yaml文件、环境变量、命令行参数

@Value("${MAVEN_HOME}") //可以读取环境变量中maven的安装路径
private String home
配置文件查找位置

1.classpath 根目录

2.classpath 根路径下的config目录

3.jar包当前目录

4.jar包当前目录的config目录

5./config子目录的直接子目录

配置文件加载顺序:
  1. 当前jar包内部的application.properties和application.yml

  2. 当前jar包内部的application-{profile}.properties 和 application-{profile}.yml

  3. 引用的外部jar包的application.properties和application.yml

  4. 引用的外部jar包的application-{profile}.properties 和 application-{profile}.yml

指定环境优先、外部优先、排下面的可以覆盖上面的同名配置项

自定义starter细节 1.Starter启动原理

starter-pom 引入autoConfigurer 包

2.自定义starter

创建一个空项目 里面有一个hello-spring-boot-starter和一个hello-spring-boot-starter-autoconfigurer
hello-spring-boot-starter中什么都不用写,只需在pom.xml中


    
        com.study
        hello-spring-boot-starter-autoconfigurer
        0.0.1-SNAPSHOT
    

hello-spring-boot-starter-autoconfigurer

先写一个配置文件Bean

@ConfigurationProperties ("study.starter")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

再写一个service

public class HelloService {

    @Autowired
    HelloProperties helloProperties;
    public String sayHello(String username){
        return helloProperties.getPrefix() + ":" +username+ helloProperties.getSuffix();
    }
}

然后写一个自动配置类

@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class) //默认HelloProperties会放在容器中
public class HelloServiceAutoConfiguration {
    @Bean
    public HelloService helloService(){
         HelloService helloService = new HelloService();
         return helloService;
    }
}

因为springboot会去meta-INF/FACTORIES寻找需要自动装配的类,所以还需要在类路径下写一个meta-INF/FACTORIES

#Auto Configure
com.study.hellospringbootstarterautoconfigurer.auto.HelloServiceAutoConfiguration

然后clean-install

创建一个测试项目

pom文件中导入hello-spring-boot-starter

写一个properties

study.starter.prefix=JOU
study.starter.suffix=Hello

controller

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String sayHello(){
         String s = helloService.sayHello("zhangsan");
         return s;
    }
}

如果在测试类中写一个新的config,那么springboot就会走myconfig中的helloservice

@Configuration
public class MyConfig{
    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        return helloService
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678293.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号