Spring boot学习(1)

1.介绍:

spring-boot是基于spring的一个用于快速开发spring应用的框架,核心思想是“约定优于配置”,避免了复杂的配置工作,目的是让开发者专注于业务。
详细请看官网:http://projects.spring.io/spring-boot/


2.特点:

  1. 创建独立的Spring应用程序
  2. 嵌入的Tomcat,无需部署WAR文件
  3. 简化Maven配置
  4. 自动配置Spring
  5. 提供生产就绪型功能,如指标,健康检查和外部配置
  6. 绝对没有代码生成和对XML没有要求配置[1]

3.入门实例:

sb框架使用非常简单,只需要2步即可完成:
(1).配置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.inesv</groupId>
    <artifactId>spring_boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</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>

    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>

(2).编写业务代码(controller):

package spring_boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {

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

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

最后,直接运行main程序即可,然后通过浏览器访问:
localhost:8080,这里不需要项目名称,因为内置tomcat默认的项目路径为根目录。。


总结:
 
1. @RestController 和@RequestMapping 注解
@RestController :指定class为controller,并且将结果直接返回给调用者,
 相当于SpringMvc中的:@Control+@ReseponseBody的效果。(个人 理解).
@RequestMapping:指定请求路径。        
        

2. @EnableAutoConfiguration 注解
  启用自动配置,这个注解结合pox.xml中指定的启动器(Starts)一起使用,比如我们上面指定了spring-boot-starter-web启动器,那么当我们加入@EnableAutoConfiguration时,程序会自动将项目配置成web应用。
3. Main方法代理运行springBoot.
4. 如何将该demo打包?
 a.在pom.xml中添加如下:

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

 b.进入项目根目录,打开命令行,输入以下命令,即可:
 

mvn package

猜你喜欢

转载自blog.csdn.net/m0_37754981/article/details/78512649