springBoot开始(一)

Author Avatar
cuteximi 7月 02, 2017
  • 在其它设备中阅读本文章

开始

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features

  • Create stand-alone Spring applications

  • Embed Tomcat,Jetty or Undertow directory(no need to deploy WAR files)

  • Automasticclly configure Spring whenever possible

  • Provide production-ready features such as metrics,health checks and externalized configuration

  • Absolutely no code generation and no description of all the features,plus an extensive howto for common user cases.

什么是spring boot

Spring Boot 充分利用了 JavaConfig 的配置模式以及“约定优于配置”的理念,能够极大的简化基于 Spring MVC 的 Web 应用和 REST 服务开发。对于已经熟悉 Spring 生态系统的开发人员来说,Spring Boot 是一个很理想的选择。
Spring Boot提供了很多”开箱即用“的依赖模块,都是以spring-boot-starter-xx作为命名的。下面列举一些常用的模块。

spring的相关模块

Spring Boot提供了很多”开箱即用“的依赖模块,都是以spring-boot-starter-xx作为命名的。下面列举一些常用的模块。

  • spring-boot-starter-logging :使用 Spring Boot 默认的日志框架 Logback。
  • spring-boot-starter-log4j :添加 Log4j 的支持。
  • spring-boot-starter-web :支持 Web 应用开发,包含 Tomcat 和 spring-mvc。
  • spring-boot-starter-tomcat :使用 Spring Boot 默认的 Tomcat 作为应用服务器。
  • spring-boot-starter-jetty :使用 Jetty 而不是默认的 Tomcat 作为应用服务器。
  • spring-boot-starter-test :包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。
  • spring-boot-starter-aop :包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。
  • spring-boot-starter-security :包含 spring-security。
  • spring-boot-starter-jdbc :支持使用 JDBC 访问数据库。
  • spring-boot-starter-redis :支持使用 Redis。
  • spring-boot-starter-data-mongodb :包含 spring-data-mongodb 来支持 MongoDB。
  • spring-boot-starter-data-jpa :包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。
  • spring-boot-starter-amqp :通过 spring-rabbit 支持 AMQP。
  • spring-boot-starter-actuator : 添加适用于生产环境的功能,如性能指标和监测等功能。

Java Config 自动配置

Spring Boot 推荐采用基于 Java Config 的配置方式,而不是传统的 XML。例如,@Configuration、@Bean、@EnableAutoConfiguration、@CompomentScan、@PropertySource、@Repository、@Service、@RestController等。

快速开始

1.新建一个空的maven项目
2.修改pom.xml,添加:


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <dependencies>
        <!--开始搭建spring boot应用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
        <!--创建web项目要加入的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
    </dependencies>

3.新建java demo文件:


package com.cuteximi.go;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by 陶世磊 on 2017/7/2.
 *
 * @Description:
 */
@SpringBootApplication
public class ApplicationDemo {
    public static void main(String[] args) throws Exception{
        System.out.println("SpringApplication Run!");
        SpringApplication.run(ApplicationDemo.class,args);

    }
}

4.新建contorller demo


package com.cuteximi.go;

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;

/**
 * Created by 陶世磊 on 2017/7/2.
 *
 * @Description:
 */
@RestController
@EnableAutoConfiguration
public class RestfulApplicationDemo {
    @RequestMapping("/")
    public  String home(){
        return "Hello Spring Boot Web!";
    }
    @RequestMapping("/hello")
    public String hello(){
        return new String("hello");
    }
    public static void main(String[] args) throws Exception{
        SpringApplication.run(RestfulApplicationDemo.class,args);
    }
}
注解区别
@RestController该注解包含@Controller和@ResponseBody,里面的方法无法返回jsp页面
@Controller配合视图解析器InternalResourceViewResolver使用,返回界面
@ResponseBody返回JSON,XMl或者自定义的mediaType

结尾附上pom.xml文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>firstSpringBoot</groupId>
    <artifactId>cuteximi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <dependencies>
        <!--开始搭建spring boot应用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
        <!--创建web项目要加入的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--该插件,运行“mvn package”进行打包时,会打包成一个可以直接运行的JAR文件,
            使用java -jar命令就可以直接运行-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

后面

机会是留给有准备的人的!

This blog is under a CC BY-NC-SA 3.0 Unported License
本文链接:http://blog.cuteximi.com/springBoot开始/