config

本文详细介绍了SpringBoot中四种不同的配置文件加载方法,包括通过Value注解、ConfigurationProperties注解、Bean结合ConfigurationProperties以及使用ConfigurationProperties和EnableConfigurationProperties注解的方式。每种方法都附有代码示例,帮助读者理解如何在实际项目中应用。
摘要由CSDN通过智能技术生成

spring boot加载配置文件

1.通过value("${}")

  1. pom

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
  2. application.yml

    spring:
      application:
        name: configdemo
    server:
      port: 8081
    test:
      name: tom
      password: admin123
    
  3. 启动类

    package com.zjb;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @author zhaojianbo
     */
    @SpringBootApplication
    public class ConfigDemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigDemoApplication.class);
        }
    }
    
  4. dao层

    package com.zjb.dao;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * @author zhaojianbo
     */
    @Component
    public class User {
        @Value("${test.name}")
        private String name;
        @Value("${test.password}")
        private String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
    
    
  5. controller

    package com.zjb.controller;
    
    import com.zjb.dao.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zhaojianbo
     */
    @RestController
    @RequestMapping("/config")
    public class HelloController {
    
        @Autowired
        User user;
    
        @RequestMapping("hello")
        public String hello() {
            return user.toString();
        }
    }
    

2.通过注解自动注入

  1. pom

     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
     </dependency>
    
  2. dao层

    package com.zjb.dao;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    /**
     * @author zhaojianbo
     */
    @Component
    @ConfigurationProperties(prefix = "test")
    //如果是默认的配置文件则不用写PropertySource
    @PropertySource("classpath:application.yaml")
    //@ConfigurationProperties(locations = "classpath:application.yaml", prefix = "test")
    public class Person {
        private String name;
        private String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
    
  3. controller

    package com.zjb.controller;
    
    import com.zjb.dao.Person;
    import com.zjb.dao.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zhaojianbo
     */
    @RestController
    @RequestMapping("/config")
    public class HelloController {
    
        @Autowired
        User user;
    
        @Autowired
        Person person;
    
        @RequestMapping("user")
        public String user() {
            return user.toString();
        }
    
        @RequestMapping("person")
        public String person() {
            return person.toString();
        }
    
    }
    

3.通过Bean和ConfigurationProperties

  1. @bean,表示的是一个bean,该注解放在方法上,方法表示为配置文件中的id,返回值表示配置文件中的class

  2. dao层

    package com.zjb.dao;
    
    /**
     * @author zhaojianbo
     */
    public class People {
        private String name;
        private String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
    
  3. 设置配置类

    package com.zjb;
    
    import com.zjb.dao.People;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    
    /**
     * @author zhaojianbo
     */
    @SpringBootApplication
    public class ConfigDemoApplication {
    
        @Bean
        @ConfigurationProperties(prefix = "test")
        public People peopleInstance() {
            return new People();
        }
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigDemoApplication.class);
    
        }
    }
    
  4. controller

    package com.zjb.controller;
    
    import com.zjb.dao.People;
    import com.zjb.dao.Person;
    import com.zjb.dao.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zhaojianbo
     */
    @RestController
    @RequestMapping("/config")
    public class HelloController {
    
        @Autowired
        User user;
    
        @Autowired
        Person person;
    
        @Autowired
        People people;
    
        @RequestMapping("user")
        public String user() {
            return user.toString();
        }
    
        @RequestMapping("person")
        public String person() {
            return person.toString();
        }
    
        @RequestMapping("people")
        public String people() {
            return people.toString();
        }
    
    }
    

4.ConfigurationProperties以及EnableConfigurationProperties注解

当ConfigurationProperties没有与component一起使用时,ConfigurationProperties标注的类没有加载到容器中,EnableConfigurationProperties使类可以加载到容器中

  1. dao类

    package com.zjb.dao;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * @author zhaojianbo
     */
    @ConfigurationProperties(prefix = "test")
    public class Man {
        private String name;
        private String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "Man{" +
                    "name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
    
    
  2. controller

    package com.zjb.controller;
    
    import com.zjb.dao.Man;
    import com.zjb.dao.People;
    import com.zjb.dao.Person;
    import com.zjb.dao.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zhaojianbo
     */
    @RestController
    @RequestMapping("/config")
    @EnableConfigurationProperties(Man.class)
    public class HelloController {
    
        @Autowired
        User user;
    
        @Autowired
        Person person;
    
        @Autowired
        People people;
    
        @Autowired
        Man man;
    
        @RequestMapping("user")
        public String user() {
            return user.toString();
        }
    
        @RequestMapping("person")
        public String person() {
            return person.toString();
        }
    
        @RequestMapping("people")
        public String people() {
            return people.toString();
        }
    
        @RequestMapping("man")
        public String man() {
            return man.toString();
        }
    
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值