springboot学习总结
入口类
由于Spring Boot会自动扫描@SpringBootApplication所在类的同级包(如com.example.demo)以及下级包里的所有bean。所以,官方建议入口类放在最外层的包名下,即@SpringBootApplication所在类的所在目录应包含整个程序的代码部分。
如下图所示:
自定义配置
spring boot可简化spring搭建的繁琐,但也可以根据自身项目的需求进行修改。
配置文件实现步骤:
1.新建类继承自WebMvcConfigurerAdapter可修改静态资源的各种属性
2.类使用@Configuration进行注解
配置文件
在application.properties需要对一些参数进行配置
例如如果使用了postgresql支持包,就需要在application.properties中对postgresql进行配置,如下
datasource.dbregistro.platform=postgres spring.datasource.url=jdbc:postgresql://localhost:5432/tl_test spring.datasource.username=xxx spring.datasource.password=xxx spring.datasource.driverClassName=org.postgresql.Driver
控制器
Controller
对页面控制器访问使用@Controller,在return时会自动查找templates下对应视图
RestController
对接口控制器访问时使用@RestController,在return时会返回对应json数据