SpringBoot学习(二)

mac2022-06-30  152

SpringBoot1.x环境搭建

1)项目中只需要依赖父工程spring-boot-starter-parent即可,该父工程中管理了很多springboot需要的依赖包及其版本。

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent>

relativePath的作用:

默认值为../pom.xml, 查找依赖父项目的顺序为: relativePath元素中的地址 - 本地仓库 - 远程仓库

<relativePath/> 就是配置了一个空的元素,意思是:始终从仓库中获取,不从本地路径获取。

2)添加spring-boot-starter-parent父工程后,项目如果需要使用springmvc和spring的jar包,只需要添加如下依赖:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>

 

不需要添加版本,因为版本号在父工程中统一管理了

3)写第一个入门程序

//开启Springboot的默认自动配置 @EnableAutoConfiguration @Controller public class IndexController { @RequestMapping("/") @ResponseBody public String first() { return "Hello World!"; } public static void main(String[] args) { /** * springboot的启动方法 * 第一个参数:当前文件的字节码 * 第二个参数:main方法的参数 */ SpringApplication.run(IndexController.class, args); } }

main方法启动即可,内嵌tomcat运行,端口号默认为8080,启动后页面输入localhost:8080/即可访问

如果想关闭某个第三方类自动配置,可以这样排除:@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class})

4)springboot的全局配置文件

名称必须为application.properties 或者是 application.yml,放在resources目录下或者类路径下的/config下,一般我们放在resources下;

 

转载于:https://www.cnblogs.com/cainiao-Shun666/p/11431124.html

最新回复(0)