Spring Boot(二)输出HelloWord的两种方式

一.使用Maven方式:

1.创建maven工程(jar):

新建工程,并输入相应的项目名等

2.在pom.xml文件中导入依赖spring boot相关的依赖:

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

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

3.编写主程序启动SpringBoot应用:

(1)在src-main-java里面新建一个类,在该类中加入主函数:

package com.zf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWordMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWordMainApplication.class,args);
    }
}

(2)@SpringBootApplication使用来标注一个主程序类,说明这是一个SpringBoot应用;

(3)SpringApplication.run(HelloWordMainApplication.class,args);作用是将Spring应用启动起来

4.编写相关的Controller、Service:

在主程序类的包下面新建一个类HelloController:

package com.zf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello word";
    }
}

5.运行主程序测试:

直接运行HelloWordMainApplication主程序;然后在浏览器输入
http://localhost:8080/hello 既可访问(默认端口8080)

6.简化部署:

(1)在pom.xml文件中配置插件:

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

这个插件,可以将应用打包成一个可执行的jar包

(2)在右侧栏的项目中,运行项目文件夹下面Lifecycle下面的package方法打包

(3)打包好的jar包默认放在target文件夹下面的maven-status里面第一个文件,将其复制到桌面等位置

(4)运行cmd,cd到桌面指定目录,用java - jar jar包名 即部署成功

(5)在浏览器输入http://localhost:8080/hello 既可成功访问

二.使用Spring Initializer快速创建SpringBoot应用:

1.新建项目选择Spring Initializer方式创建;

2.选择模块依赖的时候自己选择,比如选择web功能(需联网)

3.默认生成的SpringBoot项目:

(1)主程序已经生成好,我们只需要写自己逻辑;

(2)resources文件夹中目录结构:

static:保存所有的静态资源;js css images

templates:保存所有的模板页面,可以使用模板引擎

application.properties:SpringBoot应用的配置文件,可以修修改默认设置,比如端口等

三.Hello Word细解:

1.POM文件:

(1)父项目:

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

其父项目为:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-dependencies</artifactId>
	<version>1.5.9.RELEASE</version>
	<relativePath>../../spring-boot-dependencies</relativePath>
</parent>

该父项目即为真正管理SpringBoot应用里面的所以依赖版本

Spring Boot的版本仲裁中心
没有在dependencies里面管理的依赖需要写版本号,其他默认不用写版本号

(2)导入的依赖:

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

spring-boot-starter-web:

spring-boot-starter为spring-boot场景启动器;导入了Web模块正常运行所依赖的组件

SpringBoot将所有的功能场景都抽取处理,做成一个个的starters(启动器),只需要在项目里面引入这个spring-boot-starter相关场景的所有依赖都会导入进来。需要什么功能,就导入什么启动器

2.主程序类,主入口类:

(1)@SpringBootApplication:

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

@SpringBootApplication:SpringBoot应用标注在某个类上,说明这个类是SpringBoot的主配置类,就应该运行这个类的main方法来启动SpringBoot

(2)@SpringBootConfiguration和@EnableAutoConfiguration:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication

a. @SpringBootConfiguration:Spring Boot的配置类:
标注在某个类上,表示这是一个SpringBoot的配置类;
@Configuration:配置类上标注这个注解
配置类———配置文件;
配置类也是容器中的一个组件;@Component

b.@EnableAutoConfiguration: 开启自动配置功能;

@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration

@AutoConfigurationPackage:自动配置包
@Import({Registrar.class});
Spring的底层注解@import,给容器导入一个组件;导入的组建由 Registrar.class来确定;
将主配置类(@SpringBootApplication标注的类)的所在包以及下面所有子包里面的所有组件主入到Spring容器中;

@Import({EnableAutoConfigurationImportSelector.class}):
给容器中导入组件,选择器;
将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中;
会给容器导入许多自动配置类(xxxAutoConfiguration):就是给容器中容器中导入场景的所有组件,并且配置好组件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值