美文网首页
Spring Boot读取properties文件的四种方式

Spring Boot读取properties文件的四种方式

作者: 上杉丶零 | 来源:发表于2019-05-08 10:58 被阅读0次

    自定义test.properties文件

    my.name = leif
    my.age = 24
    

    方法一

    使用@value注解

    package com.fas.demo.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("/my")
    @PropertySource("classpath:test.properties")
    public class MyController {
        @Value("${my.name}")
        private String name;
        @Value("${my.age}")
        private int age;
    
        @GetMapping("/test")
        @ResponseBody
        public String test() {
            return name + " - " + age;
        }
    }
    

    方法二

    使用Environment

    package com.fas.demo.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("/my")
    @PropertySource("classpath:test.properties")
    public class MyController {
        @Autowired
        private Environment environment;
    
        @GetMapping("/test")
        @ResponseBody
        public String test() {
            return environment.getProperty("my.name") + " - " + environment.getProperty("my.age");
        }
    }
    

    方法三

    使用@ConfigurationProperties注解

    package com.fas.demo.configuration;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @ConfigurationProperties(prefix = "my")
    @PropertySource("classpath:test.properties")
    public class MyPropertiesConfiguration {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    package com.fas.demo.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.fas.demo.configuration.MyPropertiesConfiguration;
    
    @Controller
    @RequestMapping("/my")
    public class MyController {
        @Autowired
        private MyPropertiesConfiguration myPropertiesConfiguration;
    
        @GetMapping("/test")
        @ResponseBody
        public String test() {
            return myPropertiesConfiguration.getName() + " - " + myPropertiesConfiguration.getAge();
        }
    }
    

    方法四

    使用注册监听器

    package com.fas.demo.configuration;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    
    public class MyPropertiesConfiguration {
        public static Map<String, String> propertiesMap = new HashMap<String, String>();
    
        private static void processProperties(Properties properties) {
            for (Object key : properties.keySet()) {
                String keyString = key.toString();
    
                try {
                    propertiesMap.put(keyString, new String(properties.getProperty(keyString).getBytes("ISO-8859-1"), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void loadAllProperties(String propertyFileName) {
            try {
                Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
                processProperties(properties);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static String getProperty(String name) {
            return propertiesMap.get(name);
        }
    
        public static Map<String, String> getAllProperty() {
            return propertiesMap;
        }
    }
    
    package com.fas.demo.listener;
    
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    
    import com.fas.demo.configuration.MyPropertiesConfiguration;
    
    public class MyPropertiesListener implements ApplicationListener<ApplicationEvent> {
        private String propertyFileName;
    
        public MyPropertiesListener(String propertyFileName) {
            this.propertyFileName = propertyFileName;
        }
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            MyPropertiesConfiguration.loadAllProperties(propertyFileName);
        }
    }
    
    package com.fas.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import com.fas.demo.listener.MyPropertiesListener;
    
    @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication springApplication = new SpringApplication(DemoApplication.class);
            springApplication.addListeners(new MyPropertiesListener("test.properties"));
            springApplication.run(args);
        }
    }
    
    package com.fas.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.fas.demo.configuration.MyPropertiesConfiguration;
    
    @Controller
    @RequestMapping("/my")
    public class MyController {
        @GetMapping("/test")
        @ResponseBody
        public String test() {
            return MyPropertiesConfiguration.getProperty("my.name") + " - " + MyPropertiesConfiguration.getProperty("my.age");
        }
    }
    

    相关文章

      网友评论

          本文标题:Spring Boot读取properties文件的四种方式

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