开发第一个 springboot 的应用

1.版本的要求

Before we begin, open a terminal and run the following commands to ensure that you have valid versions of Java and Maven installed:

在cmd命令下,输入
java和mvn的版本

java -version
mvn -v

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Either way, you need Java SDK v1.8 or higher. Before you begin, you should check your current Java installation by using the following command:
Spring Boot is compatible with Apache Maven 3.3 or above

  1. 以上说明java的版本在1.8以上
  2. maven的版本在3.3以上

2.创建pom.xml文件

We need to start by creating a Maven pom.xml file. The pom.xml is the recipe that is used to build your project. Open your favorite text editor and add the following:

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

使用 查看没有包含web方面的依赖

mvn dependency:tree

在pom.xml 的 <dependencies></dependencies> 中添加如下的依赖:

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>

If you run mvn dependency:tree again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.

如果重新执行mvn dependency:tree ,你会看到一些关于tomcat和web相关的依赖包

3.写代码

package com.example.springboot.demo01;

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

/**  
 * 
 * @author zhouwenchen@021.com  
 * @date 2019年5月6日 下午2:43:29 
 */
@RestController
@EnableAutoConfiguration
public class Example {
	@RequestMapping("/")
	String home(){
		return "hello world";
	}
}

在这里插入图片描述

启动main函数
在浏览器中输入 localhost:8080 回车,就可以看到hello world

至此,第一个web程序也就启动了,默认的端口号是8080

4.解读注解

1.@RestController

相当于@Controller + @ResponseBody
1.如果仅仅使用@RestController注解Controller的话,则Controller中的方法无法返回jsp或html,配置视图解析器InternalResourceViewResolver,没有作用;返回json数据,不需要在方法上添加@ResponseBody的注解
2.如果需要返回指定的页面,需要使用@Controller配合视图解析器InternalResourceViewResolver 才可以;
如果需要返回json,xml或自定义的内容到页面,需要在方法上面添加注解@ResponseBody

2.@RequestMapping

The @RequestMapping annotation provides “routing” information. It tells Spring that any HTTP request with the / path should be mapped to the home method. The @RestController annotation tells Spring to render the resulting string directly back to the caller.

The@RestController and @RequestMapping annotations are Spring MVC annotations. (They are not specific to Spring Boot.)
See the MVC section in the Spring Reference Documentation for more details.

3.@EnableAutoConfiguration

这个注解,经过分析,需要使用到的@AutoConfigurationImportSelector 注解,

EnableAutoConfiguration
AutoConfigurationImportSelector
getAutoConfigurationEntry方法
getCandidateConfigurations方法

最后加载的配置文件的路径是META-INF/spring.factories

4.@SpringBootApplication

这里主要关注@SpringBootApplication注解,它包括三个注解:
@EnableAutoConfiguration:启用Spring Boot的自动配置机制
@ComponentScan:在应用程序所在的包上启用@Component扫描
@Configuration:允许在上下文中注册额外的bean或导入其他配置类

5.main解读

我们的应用程序的最后一部分是主要方法。 这只是遵循应用程序入口点的Java约定的标准方法。 我们的main方法通过调用run来委托Spring Boot的SpringApplication类。 SpringApplication引导我们的应用程序,启动Spring,然后启动自动配置的Tomcat Web服务器。 我们需要将Example.class作为参数传递给run方法,以告诉SpringApplication是主要的Spring组件。 args数组也被传递以暴露任何命令行参数。

另一种启动方式,在项目的根路径下面,执行cmd命令,也可以启动项目

mvn spring-boot:run

If you open a web browser tolocalhost:8080, you should see the following output:
hello world

6.项目打包成可执行的jar

To create an executable jar, we need to add the spring-boot-maven-plugin to our pom.xml. To do so, insert the following lines just below the dependencies

为了创建一个可执行的jar,我们需要在pom文件中,添加 spring-boot-maven-plugin依赖

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

然后在项目的根目录下面执行命令

mvn package

此时会执行maven的生命周期,在项目的target目录下面生成一个 springboot-0.0.1-SNAPSHOT.jar ,这个jar,就是可执行的jar
执行命令

jar -jar springboot-0.0.1-SNAPSHOT.jar

项目启动,在浏览器中输入localhost:8080,返回数据

hello world

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值