栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

创建一个简单的Spring boot 项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建一个简单的Spring boot 项目

创建一个简单Spring boot 项目 1.环境准备
  • JDK 1.8
  • maven 3.x 以上
  • IntellijIDEA2017或STS(官方推荐开发工具)
  • Spring boot 2.5.5
2.环境配置
  • maven 设置----给mavensetting文件配置的profile标签(目的是让maven以JDK1.8编译和运行项目)

    
      jdk-1.8
      
        true
        1.8
      
      
        1.8
        1.8
        1.8
      
    
    
    • IDEA 设置
      打开IDEA (不要直接打开项目),点击右下角的设置下拉框(Configure)中的setting,在setting 页面 点击Build,Execution,Deployment 下拉框,在点击Build Tools 下拉框 ,找到Maven ,点击Maven 配置IDEA 默认的maven设置(只要在这设置好默认maven-,以后就不需要每创建一个项目去设置一下maven配置)。

3.创建HelloSpringboot项目

​ 创建一个项目,编写一个接口,返回Hell SpringBoot字符串。

1)创建简单SpringBoot项目

​ 进入 https://start.spring.io/ 官网创建项目的引导页面创建项目一个简单的SpringBoot项目。如下图

2)添加web的maven配置

​ 添加完web模块就可以,用Springboot接口常用注解,不然无法使用。


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

3)创建一个简单的HelloController
package com.myf.cn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class HelloSpringBoot {
    @RequestMapping("/hello")
    public String HelloSpringboot(){
       return "Hello SpringBoot" ;
    }
}

(不需要配置包的扫描,试图解析器,等等)

4)打jar包的maven插件配置(简化部署方式)

​ 作用:可以将应用打包成一个可执行的jar包


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

4.HelloSpringboot项目探究 1)POM文件
	

    org.springframework.boot
    spring-boot-starter-parent
    2.5.5
     

点击“artifactId”中间内容进入到spring-boot-starter-parent项目的pom配置中也依赖了一个父配置如下:

spring-boot-starter-parent的pom中配置了,打包需要的一些插件以及项目的配置文件,详细的自己可以去研究下。

  
    org.springframework.boot
    spring-boot-dependencies
    2.5.5
  

点击“artifactId”中间的spring-boot-dependencies进入spring-boot-dependencies的pom配置中,可以看到properties里面有大量的版本,这就是Spring boot 真正的版本管理中心(这也是我们为什么用的是时候不需要关注其他jar包的版本信息,若是这里面没有的还需要自己添加maven的依赖信息)。

2)导入的依赖

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

spring-boot-starter-web:是Spring boot 场景启动器,可以导入web模块正常运行所依赖的组件。

Spring boot 有很多的starter依赖,它是将所有的功能场景都抽取出来,做成一个个的starter(启动器),项目如果需要这些starter相关场景,只需导入相应的starter就可以使用。

5.主程序类解析
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //标准是一个主程序类,说明是一个Spring Boot应用
public class HelloSpringBootApplication {
	//这是一个main方法
	public static void main(String[] args) {
        //SpringApplication的run方法传入启动类的class和main方法的参数
		SpringApplication.run(HelloSpringBootApplication.class, args);
	}
}
1.@SpringBootApplication:

​ 标注在Spring boot的主配置类,应该运行这个类的main方法其中Spring boot应用。

SpringBootApplication 注解是一个组合注解代码如下(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}
)}
)

@SpringBootConfiguration:是Spring boot 配置类注解。

​ 进入到SpringBootConfiguration注解的源码看到SpringBootConfiguration也是一个组合注解代码如下

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@documented
@Configuration
@Indexed

@Configuration :用于配置类的注解(注解的配置类等价于之前的配置文件,Spring 定义的注解),配置类也是容器中的一个组件(@Component)。

@EnableAutoConfiguration :开启Spring boot的自动配置。

​ 以前需要配置的东西,用上这个注解,就会开启自动配置功能,这样自动配置就生效了。这个注解的源码如下

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@AutoConfigurationPackage
@import({AutoConfigurationimportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

​ @AutoConfigurationPackage: 自动配置包(将主配置类-@SpringBootApplication 标注的类所在的包以及子包中的所有组件扫描到Spring 容器中),源码如下

import java.lang.annotation.documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar;
import org.springframework.context.annotation.import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@import({Registrar.class})
public @interface AutoConfigurationPackage {
    String[] basePackages() default {};

    Class[] basePackageClasses() default {};
}

​ @import 是Spring的底层注解,给容器中导入一个组件;导入组件由AutoConfigurationPackages.Registrar这个类决定。

​ AutoConfigurationPackages.Registrar 中的一个注册方法如下:可以打断点看下注册包是主配置类的包及子包。

public void registerBeanDefinitions(Annotationmetadata metadata, BeanDefinitionRegistry registry) {
            AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.Packageimports(metadata)).getPackageNames().toArray(new String[0]));
        }

@EnableAutoConfiguration的另一个注解@import({AutoConfigurationimportSelector.class})

​ AutoConfigurationimportSelector:导入那些组件选择器,将所有的需要导入的组件以全类名的方式返回,这些组件会被添加到容器中。会给容器中导入非常多的自动配置类(也就是starter 场景的配置)。

SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,ClassLoader)

​ Spring Boot 在启动时从类的路径下meta-INF/spring.factories 中获取EnableAutoConfiguration 指定的值,将这些值作为自动配置导入到容器中,自动配置就生效了。

J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-2.5.5.RELEASE.jar;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号