原来springBoot启动类是这样实现的(学习记录)

需要的依赖

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.33</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>

配置文件类

@Configuration
@ComponentScan("com.xxx")
@EnableWebMvc//重点
public class Appconfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        System.out.println("=================");
        converters.add(new FastJsonHttpMessageConverter());
    }
}

内置tomcat和设置servlet类

public class SpringApplication {
    public static void run(){
        Tomcat tomcat = new Tomcat();
        try {
            tomcat.setPort(56666);
            tomcat.addContext("/","d:\\");

            AnnotationConfigWebApplicationContext ac = new
                    AnnotationConfigWebApplicationContext();
            ac.register(Appconfig.class);

            DispatcherServlet servlet = new DispatcherServlet(ac);
            Wrapper mvc = tomcat.addServlet("/", "mvc", servlet);
            mvc.setLoadOnStartup(1);
            mvc.addMapping("*.do");


            tomcat.start();
            tomcat.getServer().await();

        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }
}

测试controller类

@Controller
public class IndexController {

    @ResponseBody
    @RequestMapping("/map")
    public Map<String,String> map(){
        Map<String,String> map = new HashMap<>();
        map.put("xxx","xx");
        return map;
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        SpringApplication.run();
    }
}

你可能感兴趣的:(java,spring,boot)