SpringBoot 中的 spring-boot-autoconfigure 模块学习

本贴最后更新于 2043 天前,其中的信息可能已经时移世改

背景

SpringBoot 中有一个 application.properties 配置文件,其中的配置怎么在应用程序中使用呢?然后对应的原理是?

初体验

直接通过 Value 读取配置

如果是一个参数,可以直接通过 Value 注解来取配置信息。

配置文件

application.properties

appconfig.dataFilePath=/tmp

Controller

package com.example.testapplication.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ValueController {

    @Value("${appconfig.dataFilePath}")
    String dataFilePath;

    /**
     * 输出结果
     *
     * @return 格式化字符串
     */
    @RequestMapping(value = "/value")
    public String sayHello() {
        return dataFilePath;
    }
}

效果

$ curl -s "localhost:8080/value"
/tmp

POJO 的配置

如果参数多了时,可以将参数封装成一个 Config Bean。

这里将配置 dataFilePath,从配置文件中读取到 AppConfigProperties 这个 Config Bean 中,然后在 Controller 中使用。

配置类

配置类 AppConfigProperties.class

package com.example.testapplication.service.appconfig;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Getter;
import lombok.Setter;

@ConfigurationProperties(prefix = "appconfig")
@Setter
@Getter
public class AppConfigProperties {
    private String dataFilePath;
}

配置类自动加载类

配置类自动加载类 AppConfigAutoConfiguration.java

package com.example.testapplication.service.appconfig;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(AppConfigProperties.class)
public class AppConfigAutoConfiguration {
}

配置文件

配置文件 application.properties 片断

appconfig.dataFilePath=/tmp

Controller

AppConfigController

package com.example.testapplication.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.testapplication.service.appconfig.AppConfigProperties;

@RestController
public class AppConfigController {

    @Autowired
    AppConfigProperties appConfigProperties;

    /**
     * 输出结果
     *
     * @return 格式化字符串
     */
    @RequestMapping(value = "/")
    public String sayHello() {
        return appConfigProperties.getDataFilePath();
    }

}

运行

$ curl -s "localhost:8080/"
/tmp

可见,读取了配置文件,然后加载到 AppConfigProperties 中,然后显示了出来。

其它

如果不想取 application.properties 中的配置,而是自定义配置文件名,也是可以使用 PropertySource 注解来指定的。(测试未通过,待更正。。。)

package com.example.testapplication.service.appconfig;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import lombok.Getter;
import lombok.Setter;

@ConfigurationProperties(prefix = "appconfig")
@PropertySource(value = "appconfig.properties")
@Setter
@Getter
public class AppConfigProperties {
    private String dataFilePath;
}

service bean 的配置

如果不仅仅是读配置,而是要初始化对应的 service bean 呢?

这里写一个 Service Bean,如果在 application.properties 中配置了,则使用配置的参数;否则使用默认参数。

Service

package com.example.testapplication.service.echo;

import lombok.Setter;

@Setter
public class EchoService {
    /**
     * 文件内容
     */
    private String text;

    /**
     * 是否显示文本
     */
    private boolean show;

    public String echo() {
        return show ? text : "******";
    }
}

配置类

EchoProperties.java

package com.example.testapplication.service.echo;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Getter;
import lombok.Setter;

// 配置前缀为echo
@ConfigurationProperties(prefix = "echo")
@Setter
@Getter
public class EchoProperties {

    /**
     * text默认值, Hello, World.
     */
    private String text = "Hello, World";

    /**
     * show默认值, true
     */
    private boolean show = true;

}

自动配置类

EchoAutoConfiguration.java

package com.example.testapplication.service.echo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(EchoProperties.class)
@ConditionalOnClass(EchoService.class)
@ConditionalOnProperty(prefix = "echo", // 存储配置前缀echo
    value = "enabled", // 开启
    matchIfMissing = true // 缺失检查
)
public class EchoAutoConfiguration {

    @Autowired
    private EchoProperties echoProperties;

    /**
     * 缺失EchoService Bean时,刚自动创建默认的.
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(EchoService.class)
    public EchoService echoService() {
        System.out.println("use DEFAULT EchoService");
        EchoService echoService = new EchoService();
        echoService.setShow(echoProperties.isShow());
        echoService.setText(echoProperties.getText());
        return echoService;
    }
}

配置文件

application.properties

echo.text=Hello Note
echo.show=true

控制类

EchoController.java 用来测试

package com.example.testapplication.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.testapplication.service.echo.EchoService;

@RestController
public class EchoController {

    @Autowired
    EchoService echoService;

    /**
     * 测试访问地址/hello
     *
     * @return 格式化字符串
     */
    @RequestMapping(value = "/echo")
    public String echo() {
        return echoService.echo();
    }
}

效果

$ curl -s "localhost:8080/echo"
Hello Note

starter 的自动配置

参考

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    941 引用 • 1458 回帖 • 150 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...