SpringBoot SpringBoot入门程序--HelloWorld

springboot入门程序–HelloWorld

一、环境准备

–jdk1.8:Spring Boot 推荐jdk1.7及以上;java version “1.8.0_112”
–maven3.x:maven 3.3以上版本;Apache Maven 3.3.9
–IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64、STS
–SpringBoot 1.5.9.RELEASE:1.5.9;

#1、maven设置

给maven 的settings.xml配置文件的profiles标签添加

<profile>
	<id>jdk‐1.8</id>
	<activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
	</activation>
	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	</properties>
</profile>

2.Idea设置

在这里插入图片描述

二、SpringBoot程序之----HelloWorld

1.创建一个maven工程(最后打包成可执行jar包)

2.导入springboot相关依赖

 <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.编写一个main主程序:启动SpringBoot的应用

/**
* SpringBootApplication  我们用来标注一个主程序类
*    表明是springboot应用
*/
@SpringBootApplication
public class HelloWorldApplication {
 public static void main(String[] args) {
     // Spring的启动程序
     SpringApplication.run(HelloWorldApplication.class,args);
 }
}

4.编写相关的controller和Service

@Controller
public class HelloWorldController {
    @RequestMapping(value = "/hello")
    @ResponseBody
    public String hello(){
        return "Hello World,This is my first SpringBoot Application";
    }
}

5.运行main方法,运行程序在这里插入图片描述

6.打一个可执行jar包,简化部署在这里插入图片描述在这里插入图片描述

三、入门程序之helloWorld执行研究

1.pom文件
	<!--pom文件中starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <!--starter-parent的父项目,他来真正的管理springboot中的所有版本依赖-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-dependencies</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath>../../spring-boot-dependencies</relativePath>
	</parent>
	<artifactId>spring-boot-starter-parent</artifactId>

SpringBoot版本仲裁中心,以后我们在导入版本默认不需要的版本,没有在dependencies中依赖的,我们就需要什么版本号

2.Spring Boot 启动器
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

spring-boot-starter-web: Spring Boot场景启动器,会自动帮我们导入启动web模块正常运行所需要的组件
官网地址: https://docs.spring.io/spring-boot/docs/1.5.19.RELEASE/reference/htmlsingle/
在这里插入图片描述
功能介绍: spring boot 将所有的功能场景抽取出来,做成一个的启动器(starter),只需要在项目中引入这些启动器,相关场景所需要的各种依赖也会被导入进来,要用什么功能就要用哪种场景的启动器

3.程序入口,主程序类(注解解析)
/**
* SpringBootApplication  我们用来标注一个主程序类
*    表明是springboot应用
*/
@SpringBootApplication
public class HelloWorldApplication {
   public static void main(String[] args) {
       // Spring的启动程序
       SpringApplication.run(HelloWorldApplication.class,args);
   }
}

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

@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 {

@Configuration
public @interface SpringBootConfiguration {

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

@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
}


@SpringBootConfiguration: 标注在某个类上,说明这是一个 spring boot 的配置类
@Configuration: 配置类–配置文件,配置类也是容器中的一个组件,相当于注解 组件注解@Compnent
@EnableAutoConfiguration: 开启自动配置功能。以前我们需要用xml手动配置,现在 spring boot 帮我们自动进行配置,此注解告诉 spring boot 开启自动配置功能
@AutoConfigurationPackage: 自动配置包
@Import(AutoConfigurationPackages.Registrar.class): 将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;
@Import(EnableAutoConfigurationImportSelector.class):导入哪些组件的选择器;
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
加载:
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的东西,自动配置类都帮我们;
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;

猜你喜欢

转载自blog.csdn.net/m0_38068812/article/details/88069575
今日推荐