zoukankan      html  css  js  c++  java
  • springboot自定义常量配置

    现在你建一个类:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * Created by ming on 2016/10/24.
     * locations={} 可以引入其余的属性文件
     */
    
    @ConfigurationProperties(prefix = "constant",locations={classpath:Constant.properties})
    public class Constant {
    
        private String name;
    
        private String sex;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    }

    然后在你的配置属性文件加上:

    #your constant
    constant.name=ming
    constant.sex=男

    然后在spring启动类上加上配置,目的是为了注入:

    import javax.servlet.Filter;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.context.embedded.FilterRegistrationBean;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.boot.context.web.SpringBootServletInitializer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import org.springframework.web.filter.CharacterEncodingFilter;
    import org.springframework.web.filter.HttpPutFormContentFilter;
    import org.springframework.web.multipart.commons.CommonsMultipartResolver;
    
    import com.foreveross.web.weixin.Constant;
    
    
    //exclude表示自动配置时不包括Multipart配置
    @EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
    @SpringBootApplication
    @EnableTransactionManagement
    @EnableConfigurationProperties(Constant.class)
    public class Application extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    
    
        @Bean
        public FilterRegistrationBean filterRegistrationBean() {
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
            characterEncodingFilter.setEncoding("UTF-8");
            registrationBean.setFilter(characterEncodingFilter);
            return registrationBean;
        }
    
        @Bean
        public Filter initializeHttpPutHandler() {
            return new HttpPutFormContentFilter();
        }
    
        @Bean
        public CharacterEncodingFilter initializeCharacterEncodingFilter() {
            CharacterEncodingFilter filter = new CharacterEncodingFilter();
            filter.setEncoding("UTF-8");
            filter.setForceEncoding(true);
            return filter;
        }
    
        @Bean(name = "multipartResolver")
        public CommonsMultipartResolver multipartResolver() {
            CommonsMultipartResolver resolver = new CommonsMultipartResolver();
            resolver.setDefaultEncoding("UTF-8");
            resolver.setResolveLazily(true);// resolveLazily属性启用是为了推迟文件解析
            resolver.setMaxInMemorySize(40960);
            resolver.setMaxUploadSize(2 * 1024 * 1024);// 上传文件大小 2M 50*1024*1024
            return resolver;
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    
    }

    最后使用如下:

    @Autowired
        private Constant constant;
    
        @RequestMapping(value = "/test")
        public void test() {
            System.out.println(constant.getName() + constant.getSex());
        }

    然后常量就能访问了。这个常量好处就是你可以根据不同的属性文件配置,在生产或者开发文件配置不同的变量值了。。。

    so  day day up...

  • 相关阅读:
    Elastic Search(一)
    嵌入式jetty
    mybatis中的#{}和${}的区别
    拦截器和过滤器的区别
    springboot对拦截器的支持
    Springboot对filter的使用
    springboot对监听器Listener的使用
    随机数的基本概念
    hashset和treeset区别
    java中常见的api方法记录
  • 原文地址:https://www.cnblogs.com/huzi007/p/5993626.html
Copyright © 2011-2022 走看看