美文网首页
[Java]SpringBoot - 0 前言

[Java]SpringBoot - 0 前言

作者: __Liu__ | 来源:发表于2017-02-09 16:37 被阅读0次

    1.什么是SpringBoot
    Spring从开始繁杂的XML配置,每个Bean都是一个标签,每个组件之间的关系都是基于xml,感觉开发个功能一半的时间都在配xml,到后来加入了Annotation注解,免除了大部分xml配置,但是如同 component scan/annotation-driven等还是需要配置xml。然而SpringBoot就内置了绝大部分的常规配置,让一个程序能最快的在Spring下跑起来。


    2.对开发环境的要求
    推荐 IDE+Maven


    3.一个HelloWorld程序

    3.1新建一个Maven工程
    3.2pom加入官方推荐的SpringBoot parent,以及mvc与aop(暂时用不上)所需的web

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <dependencies>
        <!-- 对mvc aop的支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <dependencies>
    

    3.3 新建一个App类,嗯,就可以跑了

    @RestController  
    @SpringBootApplication  
    public class App {  
        
      @RequestMapping("/")  
      public String hello(){  
        return "Hello world!";  
      }  
        
      public static void main(String[] args) {  
        SpringApplication.run(App.class, args);  
      }  
    }
    

    此处注意一点,RestController注解返回的都是JSON字符串数据,也可以直接写RESTFul的接口,等于给每一个方法都增加了ResponseBody注解。
    其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。也就是这个注解极大减轻了xml配置。

    访问你的localhost:8080/看看吧!

    相关文章

      网友评论

          本文标题:[Java]SpringBoot - 0 前言

          本文链接:https://www.haomeiwen.com/subject/dztwittx.html