美文网首页
SpringBoot项目配置放项目外部【3】

SpringBoot项目配置放项目外部【3】

作者: 伊夫_艾尔斯 | 来源:发表于2020-03-07 18:02 被阅读0次

    加载外部配置文件到项目中

    实现外部配置在Java代码中使用

    1. 增加一个配置文件读取类,设置读取配置的前缀\color{blue}{config.out}
    • 这种方式可以读取SpringBoot配置文件中任意的配置项,设置不同的前缀即可(\color{red}{前缀必填});
    • 虽然前面的loader和这个类可以把3个注解放一起,少一个类,但会导致这个类被加载2次还是会生产2个bean,获取ApplicationOutConfig 不是单例而导致其他问题,所以建议使用2个类。
    package com.skxe.demo.config;
    
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties("config.out")
    public class ApplicationOutConfig {
    
        private Integer serverPort;
        private String serverName;
    
        public Integer getServerPort() {
            return serverPort;
        }
    
        public void setServerPort(Integer serverPort) {
            this.serverPort = serverPort;
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("                           ------->>> 项目Main方法运行端口: " + serverPort +" <<<-------");
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
    
        }
    
        public String getServerName() {
            return serverName;
        }
    
        public void setServerName(String serverName) {
            this.serverName = serverName;
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("                           ------->>> 项目名称: "+ serverName +" <<<-------");
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
    
        }
    }
    
    
    • 重启项目,查看打印日志会有
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                               ------->>> 项目Main方法运行端口: 8003 <<<-------
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                               ------->>> 项目名称: 项目外部文件测试 <<<-------
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    • 利用接口查看配置
    @RequestMapping("/hello")
    @RestController
    public class Hello {
    
        @Autowired
        ApplicationOutConfig config;
    
    
        @RequestMapping("/test/{pathName}")
        public String test(@PathVariable("pathName") String pathName, String parmName){
            return "hello pathName | " + pathName + " ; parmName | " + parmName;
        }
    
        @RequestMapping("/test/config")
        public String testConfig(){
            return "hello  | " + config.getServerName();
        }
    
    }
    

    返回结果:hello | 项目外部文件测试

    2. 直接使用注解\color{red}{org.springframework.beans.factory.annotation.Value}取值
    @RequestMapping("/hello")
    @RestController
    public class Hello {
    
        @Value("${config.out.server-name}")
        String serverName;
    
        @Autowired
        ApplicationOutConfig config;
    
    
        @RequestMapping("/test/{pathName}")
        public String test(@PathVariable("pathName") String pathName, String parmName){
            return "hello pathName | " + pathName + " ; parmName | " + parmName;
        }
    
        @RequestMapping("/test/value")
        public String testValue(){
            return "hello  | " + serverName;
        }
    
        @RequestMapping("/test/config")
        public String testConfig(){
            return "hello  | " + config.getServerName();
        }
    
    }
    

    返回结果:hello | 项目外部文件测试

    相关文章

      网友评论

          本文标题:SpringBoot项目配置放项目外部【3】

          本文链接:https://www.haomeiwen.com/subject/sxqhdhtx.html