【Spring Boot学习之一】Spring Boot简介

环境
  Java1.8
  Spring Boot 1.3.2java

1、Spring Boot特色
一、使用java运行项目,内置tomcat,无需外部容器;
二、减小XML配置,使用properties文件和注解;
三、Spring Boot对各个Spring技术进行了封装,并非产生新技术,方便使用了Spring;web

趋势:面向服务架构(SOA)转向微服务架构,
四、Spring Cloud基于Spring Boot,Spring Cloud(HTTP+rest),基于Spring Boot中WEB组件(Spring MVC);spring

2、入门项目
一、搭建json

new-->maven project浏览器

二、依赖
利用Maven继承依赖的特性,向上层级引入Spring Boot的其余依赖包。
(1)spring-boot-starter-parent做用
在pom.xml中引入spring-boot-start-parent,spring官方的解释叫什么stater poms,它能够提供dependency management,也就是说依赖管理,引入之后在申明其它dependency的时候就不须要version了,后面能够看到。
(2)spring-boot-starter-web做用
SpringWEB核心组件(springMVC + Spring)
(3)spring-boot-maven-plugin做用
若是咱们要直接Main启动spring,那么如下plugin必需要添加,不然是没法启动的。若是使用maven 的spring-boot:run的话是不须要此配置的。(我在测试的时候,若是不配置下面的plugin也是直接在Main中运行的。) tomcat

三、编写HelloWord服务架构

package com.wjy.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

}

 

(1)@RestController
在上加上RestController 表示修饰该Controller全部的方法返回JSON格式,直接能够编写Restful接口  至关于@Controller+@ResponseBody app

(2)@EnableAutoConfiguration
做用在于让Spring Boot根据应用所声明的依赖来对Spring框架进行自动配置:
这个注解告诉Spring Boot根据添加的jar依赖猜想你想如何配置Spring。因为spring-boot-starter-web添加了Tomcat和Spring MVC,因此auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。框架

(3)SpringApplication.run(HelloController.class, args) 标识为启动类maven

(4)@ResponseBody 用在方法上将返回内容转换成json格式

(5)@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
做用:启动项目 整合经常使用注解 扫包做用(扫描当前同级包及其子包)
@ComponentScan默认状况下:它扫描全部类型(@Service,@Repository,@Component,@Controller),而且扫描范围是@ComponentScan注解所在配置类包及子包的类

四、启动
默认端口8080
(1)启动方式一  启动单个controller

package com.wjy.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

}

 

(2)启动方式二 扫包启动一片controller

package com.wjy.base;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.wjy.controller")
public class App {

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

}

 

浏览器访问验证:

 

相关文章
相关标签/搜索