准备好迎接你的大厂offer了吗?Springboot自动配置原理?安排!

在这里插入图片描述

准备好迎接你的大厂offer了吗?Springboot自动配置原理?安排!

Springboot自动配置原理

在引擎盖下,自动配置是使用标准@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");
    }
}

分类: [Springboot]
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值