Spring Boot 学习起步 七、Configuration配置


“Yeah It’s on. ”

下面是项目中一些常用的Configuration:

  • Spring MVC

    1
    2
    3
    4
    @Bean
    public DispatcherServlet dispatcherServlet() {
    return new DispatcherServlet();
    }
  • CharacterEncoding

    1
    2
    3
    4
    5
    6
    7
    @Bean
    public CharacterEncodingFilter characterEncodingFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding(characterEncoding);
    filter.setForceEncoding(true);
    return filter;
    }
  • RestTemplate

    1
    2
    3
    4
    @Bean
    public RestTemplate restTemplate() {
    return new RestTemplate();
    }
  • Filter

    1
    2
    3
    4
    5
    6
    7
    @Bean
    public FilterRegistrationBean appFilter() {
    FilterRegistrationBean reg = new FilterRegistrationBean();
    reg.setFilter(new AppFilter());
    reg.addUrlPatterns("/*");
    return reg;
    }
  • MessageConverter

    1
    2
    3
    4
    5
    6
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(
    Charset.forName(characterEncoding));
    return stringHttpMessageConverter;
    }
  • JSON转换

    1
    2
    3
    4
    5
    6
    7
    @Bean
    public ObjectMapper jsonMapper(){
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    return objectMapper;
    }
  • 数据源,以Druid为例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    @Bean(initMethod="init",destroyMethod="close")
    @ConfigurationProperties(prefix="datasource.druid")
    public DataSource dataSource() {
    return new DruidDataSource();
    }

    @Value("${datasource.druid.allow}")
    private String druidAllowUrl;

    @Value("${datasource.druid.deny}")
    private String druidDenyUrl;

    @Bean
    public ServletRegistrationBean statViewServlet() {
    ServletRegistrationBean reg = new ServletRegistrationBean();
    reg.setServlet(new StatViewServlet());
    reg.addUrlMappings("/druid/*");
    reg.addInitParameter("allow", druidAllowUrl);
    reg.addInitParameter("deny", druidDenyUrl);

    return reg;
    }
    1
    2
    3
    4
    5
    6
    7
    ##application.properties
    datasource.druid.url=jdbc:oracle:thin:@192.168.1.111:1521:xxx
    datasource.druid.username=xxx
    datasource.druid.password=xxx
    datasource.druid.filters=stat
    datasource.druid.allow=192.168.0.1/24,192.168.0.212
    datasource.druid.deny=192.168.200.1/24
  • RequestContextListener

    1
    2
    3
    4
    5
    6
    @Bean
    public RequestContextListener requestContextListener(){
    RequestContextListener requestContextListener = new RequestContextListener();

    return requestContextListener;
    }
  • 上传文件,文件大小限制

    1
    2
    3
    #写在application.properties 文件中
    #org.springframework.boot.autoconfigure.web.MultipartProperties
    multipart.maxFileSize=10Mb
  • 设置项目端口号

    1
    2
    3
    #写在application.properties 文件中
    # 以5100为项目端口号
    server.port=5100

文章作者: Hux
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Hux !
  目录