1. 程式人生 > 資訊 >已有使用者搶先買到蘋果 iPhone 12/Pro MagSafe 充電寶:有點厚,但邊緣圓潤握感舒服,磁吸力很強

已有使用者搶先買到蘋果 iPhone 12/Pro MagSafe 充電寶:有點厚,但邊緣圓潤握感舒服,磁吸力很強

自動配置原理

自動配置原理

在引擎蓋下,自動配置是使用標準@Configuration類實現的。附加@Conditional註釋用於限制何時應應用自動配置。
通常,自動配置類使用@ConditionalOnClass@ConditionalOnMissingBean註解。這確保自動配置僅在找到相關類並且您尚未宣告自己的類時才適用@Configuration

擴充套件檢視解析器

package com.kuang.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

//如果你想diy一些定製化的功能,只要寫這個元件,然後將它交給springboot接管,springboot就會幫我們自動裝配
//如果你想保持配置,並且想新增配置需要用到MVC configuration
//擴充套件SpringMvc   走dispatchservlet
@Configuration
@EnableWebMvc
public class MyMvcConfig implements WebMvcConfigurer {
    //ViewResolver 實現了試圖解析器介面類,我們可以把它看成試圖解析器

    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }

    //自定義一個自己的試圖解析器靜態內部類
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}
//如果我們要拓展springmvc,官方建議我們新增@Configuration註解
@Configuration
@EnableWebMvc   //這個註解就是匯入一個類:DelegationWebMvcConfiguration:從容器中獲取所有的WebMvcConfig
public class MyMvcConfig implements WebMvcConfigurer {
    //試圖跳轉
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/kuang").setViewName("test");
    }
}