SpringBoot常用配置简介

SpringBoot常用配置简介

1. SpringBoot中几个常用的配置的简单介绍
  • 一个简单的Spring.factories

     # Bootstrap components
     org.springframework.cloud.bootstrap.BootstrapConfiguration=\
     org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapConfiguration
    
     # Application listeners
     org.springframework.context.ApplicationListener=\
     org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapApplicationListener      
     # Autoconfiguration
     org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
     org.springframework.cloud.config.server.config.EncryptionAutoConfiguration,\
     org.springframework.cloud.config.server.config.SingleEncryptorAutoConfiguration
  • BootstrapConfiguration简介
    Spring.factories中的配置项, org.springframework.cloud.bootstrap.BootstrapConfiguration,会在Spring启动之前的准备上下文阶段将其加入到容器中。我们在介绍 @Configuration配置解析一文中曾详细解释Configuration中的解析。BootstrapConfiguration是在刷新(refresh)阶段将class作为预选的配置类@Configuration注入到容器中的,在@Configuration配置解析阶段会解析此class。

  • 加载BootstrapConfiguration配置的类
List<String> names = SpringFactoriesLoader
                .loadFactoryNames(BootstrapConfiguration.class, classLoader);
        for (String name : StringUtils.commaDelimitedListToStringArray(
                environment.getProperty("spring.cloud.bootstrap.sources", ""))) {
            names.add(name);
        }
        // TODO: is it possible or sensible to share a ResourceLoader?
        SpringApplicationBuilder builder = new SpringApplicationBuilder()
                .profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF)
                .environment(bootstrapEnvironment)
                .properties("spring.application.name:" + configName)
                .registerShutdownHook(false).logStartupInfo(false).web(false);
        List<Class<?>> sources = new ArrayList<>();
        for (String name : names) {
            Class<?> cls = ClassUtils.resolveClassName(name, null);
            try {
                cls.getDeclaredAnnotations();
            }
            catch (Exception e) {
                continue;
            }
            sources.add(cls);
        }
        builder.sources(sources.toArray(new Class[sources.size()]));
        AnnotationAwareOrderComparator.sort(sources);
        final ConfigurableApplicationContext context = builder.run();
  • 加载BootstrapConfiguration配置的类
private void prepareContext(ConfigurableApplicationContext context,ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments, Banner printedBanner) {
        context.setEnvironment(environment);
        postProcessApplicationContext(context);
        applyInitializers(context);
        listeners.contextPrepared(context);
        if (this.logStartupInfo) {
            logStartupInfo(context.getParent() == null);
            logStartupProfileInfo(context);
        }
    // Add boot specific singleton beans
        context.getBeanFactory().registerSingleton("springApplicationArguments",
                applicationArguments);
        if (printedBanner != null) {
            context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
        }

        // Load the sources
        Set<Object> sources = getSources();//获取需要加载的class源
        Assert.notEmpty(sources, "Sources must not be empty");
        load(context, sources.toArray(new Object[sources.size()]));//加载class
        listeners.contextLoaded(context);
      }
  • ApplicationListener简介
    org.springframework.context.ApplicationListener会在SpringBoot中几个典型事件产生后调用onApplicationEvent方法。详见Spring事件发布系统

  • Autoconfiguration简介
    org.springframework.boot.autoconfigure.EnableAutoConfiguration是SpringBoot中最常见配置,用于自动配置。只要有@EnableAutoConfiguration注解,该配置的所有类都会自动加载到Spring容器中。

转载于:https://www.cnblogs.com/dragonfei/p/6060419.html

在使用Spring Boot时,常用配置可以包括以下几个方面: 1. 依赖管理:在项目中使用Maven或Gradle等构建工具,通过添加依赖来管理项目所需的外部库。例如,可以使用如下配置来引入Spring Boot和Redis的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency> ``` 2. 服务器端口配置:可以通过在配置文件(比如application.properties)中设置`server.port`属性来指定应用程序运行的端口。例如: ``` server.port=8080 ``` 3. 数据库配置:如果需要使用数据库,可以在配置文件中添加相关配置,比如数据库的连接信息、用户名、密码等。根据具体的数据库类型和所使用的数据访问框架,配置方式会有所不同。 4. 日志配置:Spring Boot内置了一套简单易用的日志系统,可以通过在配置文件中配置日志的级别、输出格式等来进行日志配置。 5. 指标监控配置:如果需要对应用程序进行指标监控,可以引入`spring-boot-starter-actuator`依赖,并在配置文件中进行相应的配置,比如指定监控信息的地址、权限等。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 6. 高级配置:对于一些高级的配置需求,比如指定替换默认配置的文件位置、配置文件名等,可以在配置文件中使用`spring.config.location`、`spring.config.name`等属性进行配置。 ``` #SPRING CONFIG - 仅使用环境属性(ConfigFileApplicationListener) spring.config.additional-location = #配置除默认值之外使用的文件位置。 spring.config.location = #配置替换默认值的文件位置。 spring.config.name = application #配置文件名。 ``` 以上是Spring Boot常用配置方式,通过配置文件进行灵活的配置,以满足不同需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值