用maven创建项目后
在pom.xml文件中引入spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
引入需要的dependency依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
src.main.java目录下创建org.zhangsan包
创建MyBootApplication启动类
创建controller包,并创建IndexController控制器类
访问后出现:Whitelabel Error Page 错误
解决的办法:
1、把自建的controller类放到启动类同一级目录下(不建议这样做)。
2、把启动类@RestController @EnableAutoConfiguration注解改成@SpringBootApplication。(我使用的是这种方式)
3、启动类@RestController @EnableAutoConfiguration注解再加上@ComponentScan注解。
使用springboot扫描的两种注解配置方式:
1、@Controller
@EnableAutoConfiguration
@ComponentScan
2、@SpringBootApplication
总结:@SpringBootApplication注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan
另外:创建项目可以使用 https://start.spring.io/ 官网的项目构建工具,设置好后下载,然后导入到IDEA中。
配置文件application.yml/application.properties
优先级1-4级 逐级递减
1级:项目根目录下的config目录下 myboot/config/application.yml
2级:项目根目录下myboot/application.yml
3级:项目根目录下的src/main/resource/config目录下 myboot/src/main/resource/config/application.yml
4级:默认目录resource目录下 myboot/src/main/resource/application.yml
本文由博客群发一文多发等运营工具平台 OpenWrite 发布
网友评论