springboot项目创建笔记1 之《创建项目》

一、简介
springboot是基于spring5和JDK1.8,简化了配置和启动,特点是约定优于配置
springboot官方提供了一个快速创建应用的工具“Spring Initializr”,地址:http://start.spring.io。可以帮我们生成pom文件
springboot2.x官方文档:https://spring.io/projects/spring-boot#overview,目前看的是2.1.8
springboot是一个内嵌了web容器的,可执行程序(jar)框架

二、创建项目
1、建立一个空maven项目,选择maven-archetype-webapp
2、填入pom文件

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<!-- 核心配置,包含默认依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<groupId>com.example</groupId>
	<artifactId>myboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>myboot</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

	<dependencies>
		<!-- 模板引擎thymeleaf -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!-- web场景的依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 测试依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!-- 打包成jar的插件,项目可用java -jar xxx.jar运行 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
			</plugin> -->
		</plugins>
		<resources>
			<!-- 打包加入资源文件 -->
			<resource>
				<directory>src/main/resources</directory>
			</resource>
			<!-- 读取webapp下的静态文件 -->
			<resource>
				<directory>src/main/webapp</directory>
				<targetPath>META-INF/resources</targetPath>
			</resource>
		</resources>
		
	</build>

</project>

3、在src/main下建立java目录
4、在src/main/java下建立包com.example.myboot
5、在包com.example.myboot下建立MybootApplication.java

package com.example.myboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MybootApplication {

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

}

6、建立资源目录
在src/main/resources下建立static目录
在src/main/resources下建立templates目录
在src/main/resources下建立application.properties文件,输入:

server.port=8088

7、在src/main/webapp下建立myboot1.html

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>


三、启动项目
1、运行MybootApplication.java的main方法启动项目


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-09-29 18:19:56.119  INFO 9480 --- [           main] com.example.myboot.MybootApplication     : Starting MybootApplication on DESKTOP-PB5DAU8 with PID 9480 (D:\workspace\study\myboot\target\classes started by User in D:\workspace\study\myboot)
2019-09-29 18:19:56.121  INFO 9480 --- [           main] com.example.myboot.MybootApplication     : No active profile set, falling back to default profiles: default
2019-09-29 18:19:57.130  INFO 9480 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8088 (http)
2019-09-29 18:19:57.153  INFO 9480 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-29 18:19:57.154  INFO 9480 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-29 18:19:57.241  INFO 9480 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-29 18:19:57.241  INFO 9480 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1074 ms
2019-09-29 18:19:57.421  INFO 9480 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-29 18:19:57.510  WARN 9480 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2019-09-29 18:19:57.619  INFO 9480 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8088 (http) with context path ''
2019-09-29 18:19:57.622  INFO 9480 --- [           main] com.example.myboot.MybootApplication     : Started MybootApplication in 1.766 seconds (JVM running for 2.059)

2、浏览器访问http://localhost:8088/myboot1.html可正常显示 
3、使用jar包启动方式
java -jar myboot-0.0.1-SNAPSHOT.jar
4、关于资源文件的访问
src/main/resources下的文件打包时,放入了BOOT-INF/classes目录下
src/main/webapp下的文件打包时,放入了META-INF/resources目录下

这样使用jar包启动,可以读取到webapp下的静态文件
5、用了springboot一般不会用到jsp,前后端分离,springboot用于专门的后端应用,前端应用单独起一个。如果需要有页面,官方推荐用thymeleaf,是对jsp的一种替代
6、eclipse pom文件第一行报错Unknown    pom.xml    /myboot    line 1    Maven Configuration Problem
在pom.xml文件中的properties加入maven jar插件的版本号,但是指定为3.1.2就会报错
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>

注:最新代码上传至https://github.com/csj50/myboot

四、参考资料
https://blog.csdn.net/qq_37164847/article/details/89508910
https://blog.csdn.net/dev666/article/details/81058128

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值