spring boot 读取 application.properties 初始化bean

application.properties

bean1.hello = 你好~

bean2.name  = name
bean2.title = title

bean3.info[name]    = name
bean3.info[title]   = title
bean3.info[age]     = age

bean4.info[0] = name0
bean4.info[1] = name1
bean4.info[2] = name2
bean4.info[3] = name3

  

BeanController
@RestController
public class BeanController {

    @Value("${bean1.hello}")
    String bean1;

    @Resource
    Bean2 bean2;

    @Resource
    Bean3 bean3;

    @Resource
    Bean4 bean4;

    @RequestMapping("bean1")
    public String bean1() {
        return bean1;
    }

    @RequestMapping("bean2")
    public Bean2 bean2() {
        return bean2;
    }

    @RequestMapping("bean3")
    public Bean3 bean3() {
        return bean3;
    }

    @RequestMapping("bean4")
    public Bean4 bean4() {
        return bean4;
    }

}

  

Bean2.java
@ConfigurationProperties(prefix = "bean2")
@Component
public class Bean2 implements Serializable{
    public String name;
    public String title;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

  

Bean3.java
@ConfigurationProperties(prefix = "bean3")
@Component
public class Bean3 implements Serializable{
    private Map<String, String> info;

    public Map<String, String> getInfo() {
        return info;
    }

    public void setInfo(Map<String, String> info) {
        this.info = info;
    }
}

  

Bean4.java
@ConfigurationProperties(prefix = "bean4")
@Component
public class Bean4 implements Serializable{
    private List<String> info;

    public List<String> getInfo() {
        return info;
    }

    public void setInfo(List<String> info) {
        this.info = info;
    }
}

  

				
时间: 2024-12-06 03:31:49

spring boot 读取 application.properties 初始化bean的相关文章

Spring boot读取application.properties中文乱码

解决方案 java开发工具Idea下解决方案: File -> Settings -> Editor -> File Encodings 将Properties Files (*.properties)下的Default encoding for properties files设置为UTF-8,将Transparent native-to-ascii conversion前的勾选上. 注意:做了上面操作后,一定要重新创建application.properties,才有效. 原文地址:

spring boot项目application.properties多环境配置文件、jar包外部配置文件

一.简介 spring boot项目application.properties文件存放及使用介绍 二.方法一多环境配置文件 我们一般都会有多个应用环境,开发环境.测试环境.生产环境,各个环境的配置会略有不同,我可以根据这个创建多份配置文件,由主配置文件来控制读取那个子配置 创建spring boot项目后可以同时创建多个.properties文件,只要符合它要求的格式即可 格式:application-{profile}.properties:{profile}是变量用于自定义配置文件名称 分

Spring Boot 项目 application.properties配置说明

#======================================================================================# ★☆★☆★☆★☆★☆ spring boot 配置中心 ★☆★☆★☆★☆★☆#====================================================================================== springBoot application.properties 基

Spring boot配置文件application.properties

# =================================================================== # COMMON SPRING BOOT PROPERTIES # # This sample file is provided as a guideline. Do NOT copy it in its # entirety to your own application.               ^^^ # =====================

Spring Boot Common application properties(转载)

转自官方文档:http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 版本:1.5.4.RELEASE # =================================================================== # COMMON SPRING BOOT PROPERTIES # # This sample file is pro

spring boot 读取自定义properties文件

@Configuration@Componentpublic class PropertiesConfig { private static final String[] properties = {"/application.properties"}; private static Properties props; private static Map<Object, Object> propertiesMap = new HashMap(); public Prope

Spring boot配置文件 application.properties

# =================================================================== # COMMON SPRING BOOT PROPERTIES # # This sample file is provided as a guideline. Do NOT copy it in its # entirety to your own application.               ^^^ # =====================

附录 B. Spring Boot 配置文件 application.properties

#########COMMON SPRING BOOT PROPERTIES ######========CORE PROPERTIES=========== #SPRING CONFIG (ConfigFileApplicationListener) spring.config.name= # config file name (default to 'application') spring.config.location= # location of config file #PROFIL

Spring Boot 配置文件application.properties

#########COMMON SPRING BOOT PROPERTIES ######========CORE PROPERTIES=========== #SPRING CONFIG (ConfigFileApplicationListener) spring.config.name= # config file name (default to 'application') spring.config.location= # location of config file #PROFIL