IDEA快速创建SpringBoot项目

4 篇文章 0 订阅

一、概述

Springboot项目,java工程和web工程都可以以jar运行,所以推荐创建jar工程。

二、快速创建SpringBoot项目两种方式

Springboot只会扫描启动类当前包和以下的包。

方式一(推荐:会自动创建启动类和pom引用):

1.选中项目 > New Project > Spring Initializr > 选择Project SDK > 默认 > Next > Packaging: Jar (选择Jar启动)

Spring Boot 默认Jar使用嵌入式Tomcat,默认不支持JSP。

Mac结束查看端口:

lsof -i tcp:8080
方式二:

1.选中项目 > New Project > Maven > 选择Project SDK > 默认 > Next

GroupId:组织或者公司名称
ArtifactId:项目在组织中的唯一ID

2.pom文件继承spring-boot-starter-parent

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.baiyou</groupId>
    <artifactId>quickstart</artifactId>
    <version>1.0-SNAPSHOT</version>

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

三、单元测试

1.spring-boot-starter-test自带了JUnit Jupiter 5.x,但是Jupiter使用Gradle构建,容易报错。
2.@SpringBootTest 标签属于spring-boot-starter-test,所以保留该库。
3.引入Junit4.x库(常用),@Test 的包用org.junit.Test。
4.Web项目,Junit需要模拟ServletContext,因此测试类加上@WebAppConfiguration。

  • 1.pom引入spring-boot-starter-test
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  • 2.pom引入junit
<!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
  • 3.创建测试基类
    改写自动生成测试类为测试基类,其他测试类继承基类,其他类只需要在方法上写@Test即可。

四、日志

  • 1.无需引用
    SpringBoot集成了SLF4J,Parent 工程集成了 Logback。
    基础日志 => 直接使用(快捷方式详见…)
    系统级日志 => 详见log项目

五、编译运行

resources 目录标记为 Resources Root

  • 1.application.properies中指定spring.profile
spring.profiles.active=test
  • 2.在其他properties文件中申明spring.profile
spring.profile = dev

六、打包

  • 2.打包
mvn clean package -Dmaven.test.skip=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码上富贵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值