spring boot中常用的配置文件的重写

@Configuration
public class viewConfigSolver extends WebMvcConfigurerAdapter {
   /* spring boot 已经自动配置好了springmvc 配置好了viewResolver
    * 视图解析器(根据方法的返回值得到视图对象,视图对象决定如何渲染(转发或者重定向))
    * 我们可以自己给容器中添加一个视图解析器
    * ContentNegotiatingViewResolver就会将其组合进来*/
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        super.addViewControllers(registry);
        //浏览器发送/index.xml这个请求就来到success页面
        // 既保留了原有的配置也能使用我们自己的配置

        registry.addViewController("/index.html").setViewName("login");
        //  }
//    所有的WebMvcConfigurerAdapter组件都会一起起作用
    }
    @Bean   // 将组件注册在容器中
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
     //   这个类已经过时了,推荐的是WebMvcConfig 它也是实现了WebMvcConfig
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
               //  加上一个视图映射
                registry.addViewController("/index.html").setViewName("login");

                registry.addViewController("/main.html").setViewName("dashboard");
            }
            // 注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
               // super.addInterceptors(registry);
                //  //静态资源;  *.css , *.js 不需要再处理静态资源
              //SpringBoot已经做好了静态资源映射                              拦截所有请求   排除登录页面的请求
              registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/templates/login.html","/","/user/login","/asserts/**","/webjars/**");

            }
        };
        return adapter;
    }
    //将自定义的国际化添加到容器中
   /* @Bean
    public LocaleResolver localeResolver(){
       return new MylocaleResolver();
    } */
}

以上是自定义的视图解析器,和拦截器但是这个方法已经过时了,当你点进去查看源码的时候会发现有一个

---------------------------------------------

@Deprecated注解,科普一下这个注解的意思就是若某类或某方法加上该注解之后,表示此方法或类不再建议使用,调用时也会出现删除线,但并不代表不能用,只是说,不推荐使用,因为还有更好的方法可以调用。

或许有人会问 为什么会出现加这个注解呢,直接在写方法的时候定义一个新的不就好了吗?

因为在一个项目中,工程比较大,代码比较多,而在后续开发过程中,可能之前的某个方法实现的并不是很合理,这个时候就要新加一个方法,而之前的方法又不能随便删除,因为可能在别的地方有调用它,所以加上这个注解,就方便以后开发人员的方法调用了。
原文:https://blog.csdn.net/alinekang/article/details/79314815
--------------------------------------------------------------------------

所以这里有一个新的实现  下面这个演示是不能直接使用的,能直接使用的在下下面这个是更新后的 类 WebMvcConfigurationSupport

package cn.edu.aynu.zhulina.demo.MyConfig;

import cn.edu.aynu.zhulina.demo.component.LoginHandlerInterceptor;
import cn.edu.aynu.zhulina.demo.component.MylocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MyViewConfig extends WebMvcConfigurationSupport {
     /*自定义一个方法之后再去声明呢*/
    @Bean
     public  WebMvcConfigurationSupport webMvcConfigurationSupport(){
         WebMvcConfigurationSupport adapter = new WebMvcConfigurationSupport() {
             @Override
             protected void addViewControllers(ViewControllerRegistry registry) {
                 //super.addViewControllers(registry);
                 registry.addViewController("/").setViewName("login");
               //  加上一个视图映射
                 registry.addViewController("/index.html").setViewName("login");

                 registry.addViewController("/main.html").setViewName("dashboard");
             }

             @Override
             protected void addInterceptors(InterceptorRegistry registry) {
                 //super.addInterceptors(registry);
                // registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/","/user/login","/asserts/**","/webjars/**");

             }
         };
         return adapter;
     }

   // 将自定义的国际化添加到容器中
    @Bean
    public LocaleResolver localeResolver(){
        return new MylocaleResolver();
    }
}

但是你如果写会发现发现无法访问静态资源,已经controller都无法访问,为什么呢,是应为因为java8接口具有默认实现


因为在springboot中默认是加载了mvc的配置,可以查看注释@WebMvcAutoConfiguration,这个注释有一个条件注释@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)。也就是说只要在容器中发现有WebMvcConfigurationSupport这个类,那就会失效,我们就必须在我们的主类上添加@EnableWebMvc注解,这样我就无法访问默认的静态资源了。因为WebMvcConfigurerAdapter过时,是因为java8中接口有默认实现,而WebMvcConfigurerAdapter实现的就是WebMvcConfigurer方法,所以我只要实现WebMvcConfigurer接口,然后重写我们需要的方法即可。
原文:https://blog.csdn.net/pengdandezhi/article/details/81182701
-----------------------------------------------------

下面我带你们看一下源码,究竟是为什么

spring mvc自动配置 没事可以研究研究 

https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
        TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

    public static final String DEFAULT_PREFIX = "";

    public static final String DEFAULT_SUFFIX = "";

    private static final String[] SERVLET_LOCATIONS = { "/" };

那么就是单纯的实现类了

package cn.edu.aynu.zhulina.demo.MyConfig;

import cn.edu.aynu.zhulina.demo.component.LoginHandlerInterceptor;
import cn.edu.aynu.zhulina.demo.component.MylocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class DefineConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        /*加上一个视图映射*/
        registry.addViewController("/index.html").setViewName("login");

        registry.addViewController("/main.html").setViewName("dashboard");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/","/user/login","/asserts/**","/webjars/**");
    }
    /*将自定义的国际化添加到容器中*/
    @Bean
    public LocaleResolver localeResolver(){
        return new MylocaleResolver();
    }
}

原文地址:https://www.cnblogs.com/zhulina-917/p/10203318.html

时间: 2024-08-22 02:06:18

spring boot中常用的配置文件的重写的相关文章

spring boot中的底层配置文件application.yam(application.property)的装配原理初探

*在spring boot中有一个基础的配置文件application.yam(application.property)用于对spring boot的默认设置做一些改动. *在spring boot中有集成很多其他的包或者框架,如redis的操作的包,日志的等等. *在spring boot程序启动的时候,也就是下面这个类: @SpringBootApplicationpublic class Springboot1Application { public static void main(S

关于Spring boot中读取属性配置文件出现中文乱码的问题的解决(针对application.properties)

两种方法: 方法一:在配置文件中设置中文编码: banner.charset=utf-8server.tomcat.uri-encoding=UTF-8spring.http.encoding.charset=UTF-8spring.http.encoding.enabled=truespring.http.encoding.force=truespring.messages.encoding=UTF-8 方法二: 设置idea的编码格式 原文地址:https://www.cnblogs.com

Spring Boot中的注解

文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了解决EJB等大型企业框架对应用程序的侵入性,因此大量依靠配置文件来“非侵入式”得给POJO增加功能,然而,从Spring 3.x开始,Spring被外界最为诟病的一点就是配置繁多,号称“配置地狱”,各种xml文件,出了问题非常难排查.从Spring 4.x开始,Spring.io提供了三种方式编织B

SpringBoot(三) :Spring boot 中 Redis 的使用

前言: 这一篇讲的是Spring Boot中Redis的运用,之前没有在项目中用过Redis,所以没有太大的感觉,以后可能需要回头再来仔细看看. 原文出处: 纯洁的微笑 SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Redis还提供一些类数据库的特性,比如事务,HA,

springboot(三):Spring boot中Redis的使用

spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Redis还提供一些类数据库的特性,比如事务,HA,主从库.可以说Redis兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景.本文介绍Redis在Spring Boot中两个典型的应用场景. 如何使用 1.引入

Spring Boot中Redis的使用

软件152   高光顺 redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Redis还提供一些类数据库的特性,比如事务,HA,主从库.可以说Redis兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景.本文介绍Redis在Spring Boot中两个典型的应用场景. 如何使用 1.引入 spring-boot-starter-redis <depe

Spring boot中使用log4j

我们知道,Spring Boot中默认日志工具为logback,但是对于习惯了log4j的开发者,Spring Boot依然可以很好的支持,只是需要做一些小小的配置功能.Spring Boot使用log4j只需要一下几步 引入log4j依赖 在创建Spring Boot工程时,我们引入了spring-boot-starter,其中包含了spring-boot-starter-logging,该依赖内容就是Spring Boot默认的日志框架Logback,所以我们在引入log4j之前,需要先排除

Spring Boot中使用RabbitMQ的示例代码

很久没有写Spring Boot的内容了,正好最近在写Spring Cloud Bus的内容,因为内容会有一些相关性,所以先补一篇关于AMQP的整合. http://www.ljhseo.com/http://www.xyrjkf.net/http://www.xyrjkf.cn/http://www.xyrjkf.com.cn/http://www.zjdygsi.cn/http://www.zjdaiyun.cn/http://www.jsdygsi.cn/http://www.xyrjkf

巧用Spring Boot中的Redis

Redis 介绍 Redis 是目前业界使用最广泛的内存数据存储.相比 Memcached,Redis 支持更丰富的数据结构,例如 hashes, lists, sets 等,同时支持数据持久化.除此之外,Redis 还提供一些类数据库的特性,比如事务,HA,主从库.可以说 Redis 兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景.本文介绍 Redis 在 Spring Boot 中两个典型的应用场景. 如何使用 1.引入依赖包 <dependency> <groupId&g