《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
3. 简化 Maven 配置
- 自动配置 Spring,没有 XML 配置!
项目目录结构:
springboot 项目中必须在 src/main/resources
中 放入 application.yml
(application.properties
)核心配置文件,名字必须为:application
;
springboot 项目中必须在 src/main/java
中所有子包之外构建全局入口类型 xxApplication
,入口类一个 springboot 项目只能有一个。
==================================================================================
项目整体结构:
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
org.springframework.boot
spring-boot-starter-web
配置文件位于:src/main/resources/application.yml
;
可以通过修改端口号解决 tomcat 端口占用问题:
server:
port: 8989 #用来指定内嵌服务器端口号
context-path: /springboot #用来指定项目的访问路径
yml 基础语法
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists: # 数组
-
lisi
-
zhaoliu
dog:
name: 小狗
age: 12
@RestController
@RequestMapping(“/hello”)
public class HelloController {
@GetMapping(“/hello”)
public String hello() {
System.out.println(“hello springboot!!!”);
return “hello springboot”;
}
}
// @EnableAutoConfiguration // 作用: 开启自动配置 初始化spring环境 springmvc环境
// @ComponentScan // 作用: 用来扫描相关注解 扫描范围 当前入口类所在的包及子包(com.yusal及其子包)
// 下面的注解的功能是上面两个注解功能之和
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// springApplication: spring应用类 作用: 用来启动springboot应用
// 参数1: 传入入口类 类对象 参数2: main函数的参数
SpringApplication.run(Application.class, args);
}
}
运行 Application 中的 main 来启动整个项目:如下则启动成功。
. ____ _ __ _ _
/\ / _’ __ _ () __ __ _ \ \ \ \
( ( )___ | '_ | '| | ’ / _` | \ \ \ \
\/ _)| |)| | | | | || (| | ) ) ) )
’ || .__|| ||| |__, | / / / /
=|_|======|/=////
:: Spring Boot :: (v2.2.5.RELEASE)
2020-06-19 21:56:45.166 INFO 8888 — [ main] com.yusael.Application : Starting Application on DESKTOP-PNEMUCC with PID 8888 (D:\CodePro\IdeaPro\SpringBoot\hello_springboot\target\classes started by yusael in D:\CodePro\IdeaPro\SpringBoot)
2020-06-19 21:56:45.171 INFO 8888 — [ main] com.yusael.Application : No active profile set, falling back to default profiles: default
2020-06-19 21:56:46.716 INFO 8888 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8989 (http)
2020-06-19 21:56:46.730 INFO 8888 — [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-19 21:56:46.731 INFO 8888 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-06-19 21:56:46.855 INFO 8888 — [ main] o.a.c.c.C.[.[.[/hello_springboot] : Initializing Spring embedded WebApplicationContext
2020-06-19 21:56:46.856 INFO 8888 — [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1613 ms
2020-06-19 21:56:47.244 INFO 8888 — [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor’
2020-06-19 21:56:47.473 INFO 8888 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8989 (http) with context path ‘/hello_springboot’
2020-06-19 21:56:47.487 INFO 8888 — [ main] com.yusael.Application : Started Application in 3.253 seconds (JVM running for 4.806)
2020-06-19 21:57:28.156 INFO 8888 — [nio-8989-exec-3] o.a.c.c.C.[.[.[/hello_springboot] : Initializing Spring DispatcherServlet ‘dispatcherServlet’
2020-06-19 21:57:28.157 INFO 8888 — [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet’
2020-06-19 21:57:28.165 INFO 8888 — [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
注意:springboot 的项目默认没有项目名,项目名要在 application.yml
中配置;
如果不在配置文件中配置,路径应该是:
http://localhost:8080/hello/hello
由于我们在配置文件中进行了配置:
server:
port: 8989 #用来指定内嵌服务器端口号
context-path: /springboot #用来指定项目的访问路径
所以访问路径变成了:
http://localhost:8989/springboot/hello/hello
@EnableAutoConfiguration
- 作用:开启自动配置 ,根据 pom.xml 文件中依赖自动判断并构建环境
如在第一个环境之中引入了 spring-boot-starter-web, 会自动根据引入的这个依赖构建相关环境(springmvc环境、web容器环境)
- 修饰范围:只能用在类上
最后
我还为大家准备了一套体系化的架构师学习资料包以及BAT面试资料,供大家参考及学习
已经将知识体系整理好(源码,笔记,PPT,学习视频)
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
:只能用在类上
最后
我还为大家准备了一套体系化的架构师学习资料包以及BAT面试资料,供大家参考及学习
已经将知识体系整理好(源码,笔记,PPT,学习视频)
[外链图片转存中…(img-IK1qfwLX-1714663805998)]
[外链图片转存中…(img-vWOcxvgM-1714663805999)]
[外链图片转存中…(img-erYNSabY-1714663805999)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!