原创

精通Spring Boot教程(11):自定义配置

今天这篇文章给大家介绍自定义配置的两种方式
第一式: 使用@ConfigurationProperties,且看代码

package com.developlee.customconfig.config;

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

/**
 * @author Lensen
 * @desc
 * @since 2018/8/22 12:59
 */
@Configuration
@ConfigurationProperties(prefix = "one-app")
public class OneAppConfig {

    @NestedConfigurationProperty
    public Account account = new Account();

    public String appName;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public class Account {
        private String username;
        private String password;
        private String age;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }
}

很明显,这就是我们要在properties文件中要配置的配置项。
再看第二种方式

/**
 * @author Lensen
 * @desc
 * @since 2018/8/22 13:19
 */
@Configuration
public class TwoAppConfig {
    @Value("${two-app.welcome.message}")
    public String twoAppWelcomeMessage;

    @Value("${two-app.welcome.person}")
    public String twoAppWelcomePerson;

    public String getTwoAppWelcomeMessage() {
        return twoAppWelcomeMessage;
    }

    public void setTwoAppWelcomeMessage(String twoAppWelcomeMessage) {
        this.twoAppWelcomeMessage = twoAppWelcomeMessage;
    }

    public String getTwoAppWelcomePerson() {
        return twoAppWelcomePerson;
    }

    public void setTwoAppWelcomePerson(String twoAppWelcomePerson) {
        this.twoAppWelcomePerson = twoAppWelcomePerson;
    }
}

这个就简单粗暴啦。没有第一种方式结构那么清晰,具体怎么使用,完全取决于项目配置项的关联关系和复杂度,需要大家根据实际情况权衡。
接下来我写了个简单的测试类,来获取我们的配置信息
先看配置文件:

one-app:
  app-name: OneAPP
  account:
    username: Lensen
    password: Orcl
    age: 22

two-app:
  welcome:
    message: welcome to lensen's bolg
    person: LENSEN

一个简单的Controller类

package com.developlee.customconfig.controller;

import com.developlee.customconfig.config.OneAppConfig;
import com.developlee.customconfig.config.TwoAppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Lensen
 * @desc
 * @since 2018/8/22 16:40
 */
@RestController
public class AppController {
    @Autowired
    private OneAppConfig oneAppConfig;
    @Autowired
    private TwoAppConfig twoAppConfig;

    @GetMapping("/hello")
    public ResponseEntity getConfig() {
        String str1 = "oneAppConfig: " + oneAppConfig.getAppName() + oneAppConfig.getAccount().getUsername()
                + oneAppConfig.getAccount().getPassword() + oneAppConfig.getAccount().getAge();
        String str2 = "twoAppConfig: " + twoAppConfig.getTwoAppWelcomePerson() + twoAppConfig.getTwoAppWelcomeMessage();
        return new ResponseEntity(str1 +"~~~~~~~"+ str2, HttpStatus.OK);
    }
}

在地址栏输入http:localhost:8080/hello, 回车
spring boot
也可以自己指定文件,只需在类上加上注解@PropertySource注解就好了~~

@Configuration
@PropertySource("classpath:my.properties")
public class ThreeConfig {

    @Value("${my.name}")
    private String myName;

    public String getMyName() {
        return myName;
    }

    public void setMyName(String myName) {
        this.myName = myName;
    }
}

my.properties文件内容:

my.name=developlee

测试结果:
spring boot

如果配置文件是yml格式的,则要使用YamlPropertiesFactoryBean来加载并设置到PropertySourcesPlaceholderConfigurer中

// 加载YML格式自定义配置文件
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new FileSystemResource("config.yml"));//File引入
//        yaml.setResources(new ClassPathResource("youryml.yml"));//class引入
        configurer.setProperties(yaml.getObject());
        return configurer;

end...
浮躁的社会,浮躁的人生,唯有代码,宁静致远。(又开始装13了,见谅.....)

最后,以上示例代码可在我的github.com中找到。

正文到此结束