spring boot 源码分析(一) 案例

spring boot 源码分析(一) 案例

前言

很久以前就想写点什么,可是一直没有合适的话题。今天终于决定以spring boot作为blog生涯的开篇,一方面总结自己的理解,另一方面也和广大猿友们交流探讨,以期提升。

第一个案例

准备

源码获取地址:https://github.com/spring-projects/spring-boot
pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

demo

package per.dyp;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@SpringBootApplication
@RequestMapping("/rest")
public class HelloController {

    @RequestMapping("/sayHello/{name}")
    public String sayHello(@PathVariable String name) {
        return "Hello " + name;
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloController.class, args);
    }
}

运行main方法,浏览器输入http://localhost:8080/rest/sayHello/dyp 浏览器显示Hello dyp

@SpringBootApplication 注解源码

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AliasFor;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {

    Class<?>[] exclude() default {};

    String[] excludeName() default {};

    @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
    String[] scanBasePackages() default {};

    @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
    Class<?>[] scanBasePackageClasses() default {};
}

对注解感兴趣的可以自行研究,这里不进行表述

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/dinglovely/article/details/52693703
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞