web应用程序开发案例分析实验一 Spring Boot开发入门

实验一 Spring Boot开发入门

一、实验目的

1、掌握Spring Boot的项目构建

2、熟悉Spring Boot的自动化配置原理以及执行流程

二、实验内容

1、使用Maven方式构建Spring Boot项目

2、使用Spring Initializr方式构建Spring Boot项目

3、完成单元测试和热部署

4、掌握Spring Boot依赖管理

5、掌握Spring Boot执行流程

三、实验步骤

1、环境准备

配置完成如下环境:

JDK 1.8.0_201(及以上版本)

Apache Maven 3.6.0

IntelliJ IDEA Ultimate

2、新建Spring Boot项目

(1)使用Maven创建Spring Boot项目

① 在pom.xml中添加Spring Boot相关依赖

<parent>
	      <groupId>org.springframework.boot</groupId>
	      <artifactId>spring-boot-starter-parent</artifactId>
	      <version>2.1.3.RELEASE</version>
</parent>
<dependencies>
	<dependency>
		  <groupId>org.springframework.boot</groupId>
		 <artifactId>spring-boot-starter-web</artifactId>
	 </dependency>
</dependencies>

② 编写主程序启动类

在java文件夹下创建com.lg文件夹并创建ManualChapter01Application.java文件

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

③ 创建一个用于Web访问的Controller

在com.lg文件下创建controller文件夹并创建HelloController.java文件

@RestController   
public class HelloController {
@GetMapping("/hello")
    public String hello(){
        return "hello Spring Boot";
}

④ 运行项目

启动项目,在浏览器上访问 http://localhost:8088/hello

(2) 使用Spring Initializr创建SpringBoot项目

① 创建Spring Boot项目,并配置好项目结构

② 创建一个用于Web访问的Controller

在com.lg.ch01下创建controller文件夹并创建HelloController.java文件

@RestController   
public class HelloController {
@GetMapping("/hello")
   public String hello(){
        return "hello Spring Boot";
    }
}

③ 运行项目

④ 启动项目,在浏览器上访问 http://localhost:8088/hello

3、单元测试

① 在pom文件中添加spring-boot-starter-test测试启动器

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

② 编写单元测试类

@RunWith(SpringRunner.class) 
@SpringBootTest  
public class Chapter01ApplicationTests {
	@Test
	public void contextLoads() {
	}
}

③ 编写单元测试方法

在test\java\com.lg.ch01下的Ch01Appliction.java中编写

@Autowired
private HelloController helloController;
@Test
public void helloControllerTest() {
	String hello = helloController.hello();
	System.out.println(hello);
}

④ 运行结果

       执行测试方法helloControllerTest(),控制台输出如图。

4、热部署

① 在pom文件中添加spring-boot-devtools热部署依赖

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

② IDEA中热部署设置

选择【File】→【Settings】选项,打开Compiler面板设置页。勾选Build project automatically选项,单击Apply->OK按钮保存设置。

使用快捷键“Ctrl+Shift+Alt+/”打开Maintenance选项框,选中并打开Registry页面。勾选compiler.automake.allow.when.app.running。

③ 热部署测试

启动chapter01项目,通过浏览器访问http://localhost:8088/hello

5、Spring Boot 依赖管理

(1) spring-boot-starter-parent依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.3.RELEASE</version>
	<relativePath/>
</parent>

 (2)spring-boot-starter-web依赖

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

6、Spring Boot 执行流程

(1)初始化Spring Application实例

public SpringApplication(ResourceLoader resourceLoader,Class...primarySources){
    this.sources = new LinkedHashSet();
    this.bannerMode = Mode.CONSOLE;
    this.logStartupInfo = true;
    this.addCommandLineProperties = true;
    this.addConversionService = true;
    this.headless = true;
    this.registerShutdownHook = true;
    this.additionalProfiles = new HashSet ();
    this.isCustomEnvironment = false;
    this.resourceLoader = resourceLoader;
    Assert.notNull (primarySources, "PrimarySources must not be null") ;
    this.primarySources = new LinkedHashSet (Arrays.asList (primarySources)) ;
    this.webApplicationType = WebApplicationType. deduceFromClasspath () ;
    this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    this.setListeners(this.getSpringFactoriesInstances (ApplicationListener.class));
    this.mainApplicationClass = this. deduceMainApplicationClass();
}

(2)初始化Spring Boot项目启动

public ConfigurableApplicationContext run(String...args){
    StopWatch stopWstch = new StopWatch();
    stopWstch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter>exceptionReporters = new ArrayList();
    this.configureHeadlessProperty();
    SpringApplicationRunListeners listeners = this.getRunListeners(args);
    listeners.starting();
    Collection exceptionReporters;
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArgument(args);
        ConfigurableEnvironment environment = this.preoareEnvironment(listeners,applicationArguments);
        this.configureIgnoreBeanInfo(environment);
        Banner printedBanner = this.printBanner(environment);
        context = this.createApplicationContext () ;
        exceptionReporters = this. getSpringFactoriesInstances(SpringBootExceptionReporter.class,
        new Class [] {ConfigurableApplicationContext.class},new Object [] {context});
        this.prepareContext (context, environment, listeners, applicationArguments, printedBanner);
        this.refreshContext (context) ;
        this.afterRefresh (context, applicationArguments);
        stopWatch.stop ();
        if (this.logStartupInfo){
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopwatch);
        }
        listeners.started (context) ;
        this. callRunners (context, applicationArguments);
    } catch (Throwable var10) {
        this.handleRunFailure (context, varl0, exceptionReporters, listeners);
        throw new IllegalStateException (var10);
    }
        try
        {
            listeners.running (context);
            return context;
        } catch (Throwable var9){
            this.handleRunFailure(context,var9,exceptionReporters,(SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小孙同学1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值