快速搭建Spring Boot项目

Spring boot是Spring推出的一个轻量化web框架,主要解决了Spring对于小型项目饱受诟病的配置和开发速度问题。

Spring Boot 包含的特性如下:

创建可以独立运行的 Spring 应用。
直接嵌入 Tomcat 或 Jetty 服务器,不需要部署 WAR 文件。
提供推荐的基础 POM 文件来简化 Apache Maven 配置。
尽可能的根据项目依赖来自动配置 Spring 框架。
提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。
没有代码生成,也没有 XML 配置文件。

第一个SpringBoot应用:

1.构建maven项目
IDEA:

Create New Project -> Maven -> 选择JDK ->
GroupId : com.example, ArtifactId: springBootDemo -> Project name : springBootDemo

2.编制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>com.example</groupId>
    <artifactId>springBootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

使用父pom虽然简单,但是有些情况我们已经有父pom,不能直接增加时,可以通过如下方式:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.4.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.编制Application.java存于myFirstProject\src\main\java\com\example下

package com.example;

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

@SpringBootApplication
public class Application {

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

4.编制Example.java存于myFirstProject\src\main\java\com\example\web下

package com.example.web;

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

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    @RequestMapping("/hello/{myName}")
    String index(@PathVariable String myName) {
        return "Hello " + myName;
    }
}

5.运行
在Application.java文件上(或文件中) 选择Run 'Application '
如下图:

这里写图片描述

6.访问
运行成功后,打开浏览器,输入http://localhost:8080
页面展示Hello World!


更加快速的搭建:

spring提供了一个非常好的辅助工具,直接为我们生成Maven架构的工程。
一、在浏览器中打开http://start.spring.io/,如图

这里写图片描述

点击“Switch to the full version.”,勾选"web",然后点击“ Generate Project alt +”按钮,把文件保存到本地某个位置

二、下载文件导入IDEA,添加上文的Example

三、运行,打开浏览器,输入http://localhost:8080
页面展示Hello World!


注解详解:

Spring boot 中常用的注解如下:

@ResponseBody
用该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful的api;

@Controller
用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层)。

@RestController
@ResponseBody和@Controller的合集

@RequestMapping
提供路由信息,负责URL到Controller中的具体函数的映射。

@EnableAutoConfiguration
Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们.

@ComponentScan
表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。

@Configuration
相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。

@SpringBootApplication
相当于@EnableAutoConfiguration、@ComponentScan和@Configuration的合集。

@Import
用来导入其他配置类。

@ImportResource
用来加载xml配置文件。

@Autowired
自动导入依赖的bean

@Service
一般用于修饰service层的组件

@Repository
使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。

posted @ 2017-01-23 16:03  LSPZ  阅读(1338)  评论(0编辑  收藏  举报