spring boot 学习二

如何在Spring Boot应用程序上执行引导

Spring Initializer

引导Spring Boot应用程序的一种方法是使用Spring Initializer。 为此需要访问Spring Initializer 网页 www.start.spring.io 并选择 Build,Spring Boot版本和平台。 此外还需要提供组,工件和所需的依赖项来运行应用程序。

请注意以下屏幕截图,其中显示了添加spring-boot-starter-web依赖项以编写REST端点的示例。

spring boot 学习二_第1张图片

提供组,工件,依赖关系,构建项目,平台和版本后,单击“Generate Project”按钮。 将下载zip文件并提取文件。

本节通过使用Maven解释了这些示例。

Maven

下载项目后,解压缩文件。pom.xml 文件的内容如下所示 -



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.3.0.RELEASE
		 
	
	com.wzy
	demo-sboot
	0.0.1-SNAPSHOT
	war
	demo-sboot
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

Main方法

Main方法应该是编写Spring Boot Application类。 该类应使用@SpringBootApplication进行注释。这是启动Spring启动应用程序的入口点。以在src/java/main目录下找到主类文件。

在此示例中,主类文件位于src/java/main目录中,其默认包为com.wzy.demo-sboot。 请观察此处显示的代码以便更好地理解 -

package com.wzy.demosboot;

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

@SpringBootApplication
public class DemoSbootApplication {

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

}

编写一个Rest端点

要在Spring Boot Application主类文件本身中编写一个简单的Hello World Rest 端点,请按照以下步骤操作 -

  • 首先,在类的顶部添加@RestController注释。
  • 使用@RequestMapping注释编写Request URI方法。
  • Request URI方法应该返回Hello World字符串。
  • 现在,Spring Boot Application类文件将如下面的代码所示 -
package com.wzy.demosboot;

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

@SpringBootApplication
@RestController
public class DemoSbootApplication {

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

	@RequestMapping(value = "/")
	public String hello() {
		return "Hello World";
	}

}

通过配置tomcat进行debug调试

spring boot 学习二_第2张图片

spring boot 学习二_第3张图片

运行debug调试后看一下控制台,Tomcat在端口8080上启动。 现在,转到Web浏览器输入 http://localhost:8080/,可以看到如下所示的输出 
spring boot 学习二_第4张图片

调试运行后会在target目录下生成一个war的文件,可直接放到tomcat里面部署即可

spring boot 学习二_第5张图片

如果没有出现war文件则查看一下pom.xml里面的配置是否是war

spring boot 学习二_第6张图片

改成使用外部tomcat的web容器

正常springboot项目在创建的时候是会生成启动类的,这个类启动的是内置的tomcat容器,通过继承继承SpringBootServletInitializer,重写他的configure方法来使用外部tomcat(web容器启动项目),自己可以设置端口号,项目名。

案例

package com.wzy.demosboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoSbootApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(DemoSbootApplication.class);
	}

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

	@RequestMapping(value = "/")
	public String hello() {
		return "Hello World";
	}

}

设置Main类

在Spring Boot中,需要在构建文件中指定启动的主类。
pom.xml属性中添加start类,如下所示 -

com.wzy.demo-sboot.DemoSbootApplication

查看pom.xml 中是否添加了Spring Boot启动程序依赖项 -


   org.springframework.boot
   spring-boot-starter-web

完整代码如下:

文件:pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.3.0.RELEASE
		 
	
	com.wzy
	demo-sboot
	0.0.1-SNAPSHOT
	war
	demo-sboot
	Demo project for Spring Boot

	
		1.8
		com.wzy.demo-sboot.DemoSbootApplication
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


 

 

你可能感兴趣的:(spring,boot)