美文网首页
application.properties的包外配置

application.properties的包外配置

作者: 螃蟹和骆驼先生Yvan | 来源:发表于2018-08-06 09:22 被阅读11次

    之前有写过类似的博客写的不咋样准备删了重写一份,首先不需要什么依赖,直接一个实体类和启动类同级:

    import java.util.ResourceBundle;
    /**
     * @author qian
     * @since 2017/8/6
     */
    public class ApplicationProperties {
    
        private static ResourceBundle resourceBundle = ResourceBundle.getBundle("application");
        private static ResourceBundle resourceBundle2 = ResourceBundle.getBundle("application");
    
        public static String getText(String key) {
            try {
                return resourceBundle.getString(key);
            } catch (Exception e) {
    
            }
            return null;
        }
    
        public static String getResource(String key) {
            try {
                return resourceBundle2.getString(key);
            } catch (Exception e) {
    
            }
            return null;
        }
    }
    

    之后在配置文件配置如图:


    配置
    测试

    其实我这里还差一句没说就是包外配置的注解:@PropertySource(value="file:/home/ec2-user/webapps/application.properties")

    打包之后包外配置

    相关文章

      网友评论

          本文标题:application.properties的包外配置

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