Spring Boot 配置类深度解析
    		       		warning:
    		            这篇文章距离上次修改已过419天,其中的内容可能已经有所变动。
    		        
        		                
                
package com.example.demo.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@Configuration
public class WebConfig implements WebMvcConfigurer {
 
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/hello").setViewName("hello");
        // 这里可以添加更多的视图控制器映射
    }
 
    // 这里可以添加其他的Spring MVC配置
 
    @Bean
    public MyBean myBean() {
        return new MyBean(); // 返回自定义的Bean实例
    }
}
 
class MyBean {
    // 自定义的Bean可以在这里定义其功能
}这个代码示例展示了如何在Spring Boot应用程序中创建一个配置类,实现WebMvcConfigurer接口,并覆盖addViewControllers方法来定义视图控制器路由。同时,还展示了如何定义和注册一个自定义的Bean。这些技巧对于构建动态Web应用程序非常有用。
评论已关闭