# springboot_demo **Repository Path**: failureone/springboot_demo ## Basic Information - **Project Name**: springboot_demo - **Description**: sdfdsfdsfdsfdsfsdf - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2024-08-08 - **Last Updated**: 2024-08-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 1.SpringBoot的特点 1) 地位:Spring生态的三个其中之一(SpringFramework、SpringBoot、SpringClound) 2) 自动配置(约定大于配置)。内部已经继承了很多默认的配置,因此程序员可以将精力更多的关注于业务代码的实现 3) 起步依赖。程序员不再需要导入大量的jar依赖,也不需要担心各个jar之间的版本匹配问题。我们只需要导入springboot的XXX-starter.jar 2.SpringBoot快速入门 1) 新建了Empty Project 2) 新建了moudle 3) 指定parent 4) 导入starter 5) 新建了Controller @RestController public class HelloController{ @GetMapping("/hello") public String sayHello(){ return "hello springboot!"; } } 6) 新建了Application,提供main方法 @SpringBootApplication public class Application{ public void main(String[] args){ SpringApplication.run(Application.class,args); } } 7) @SpringBootApplication的说明 ① @SpringBootConfiguration -> @Configuration -> 说明当前类是一个配置类。相当于以前的applicationContext.xml ② @ComponentScan -> 说明当前配置类有组件自动扫描的功能。扫描当前目录,以及子(子)目录 ③ @EnableAutoConfiguration 3. 由入门程序引出的思考 1) 为什么我们在添加启动器的时候不需要在启动器的坐标中指定版本 org.springframework.boot spring-boot-starter-web 因为: 当前项目(springboot_01)的父工程是: org.springframework.boot spring-boot-starter-parent 2.3.6.RELEASE 因此:我们去查看父工程的pom文件(spring-boot-starter-parent.pom): 我们发现它里面又有一个parent标签: org.springframework.boot spring-boot-dependencies 2.3.6.RELEASE 说明:spring-boot-starter-parent的父工程是:spring-boot-dependencies 因此,我们继续查看spring-boot-dependencies.pom: org.springframework.boot spring-boot-starter-web 2.3.6.RELEASE 2) 为什么我们就添加一个启动器依赖,项目就可以运行起来 在我们的项目的pom文件中有一个dependency: org.springframework.boot spring-boot-starter-web 因此,我们查看spring-boot-starter-web.pom 在这个pom文件中有一堆的依赖: spring-boot-starter spring-boot-starter-json spring-boot-starter-tomcat spring-web spring-webmvc 4. springboot的配置文件 1) springboot支持两种类型的配置文件:properties,yml(yaml) 2) 有规定:配置文件必须放在classpath下;配置文件必须是application开头 为什么有这样的规定,依据是什么? 点击parent中的artifactId 在父工程的pom文件中,找到如下配置: ${basedir}/src/main/resources true **/application*.yml **/application*.yaml **/application*.properties 3) 程序启动的时候,端口号是8080.那么这一项是默认配置 依据:org.springframework....boot-autoconfigure 打开META-INF,打开additional-spring-configuration-metadata.json: 搜索8080: { "name": "server.port", "defaultValue": 8080 }, 4) 加载properties类型的属性配置文件 ① 准备了application.properties com.atguigu.driver=com.mysql.jdbc.Driver com.atguigu.url=.. ... ② 准备了vo对象:DataSourceProperties.java 四个私有属性 @Component public class DataSourceProperties{ @Value("${com.atguigu.driver}") private String abcd; private String url; private String username; private String pwd ; } ③ 要在Controller类中使用这个属性对象 @RestController public class HelloController{ @Autowired private DataSourceProperties dataSourceProperties; @GetMapping("/hello") public String sayHello(){ return "hello springboot!"; } } 5) 使用@ConfigurationProperties批量注入 @ConfigurationProperties(prefix="com.atgui.datasource") public class DataSourceProperties{ private String driverClassName , url , username , pwd ; } @RestController @EnableConfigurationProperties(DataSourceProperties.class) public class HelloController{ } 6) 属性批量注入+自动配置+条件化注解 举例:DefaultViewResolver - SpringApplication.run() -> spring.factories -> EnableAutoConfiguration -> WebMvcAutoConfiguration -> defaultViewResolver() defaultViewResolver()方法定义如下: @Bean @ConditionalOnMissingBean //条件化注解 public InternalResourceViewResolver defaultViewResolver() { //创建InternalResourceViewResolver InternalResourceViewResolver resolver = new InternalResourceViewResolver(); //设置前缀和后缀 //this.mvcProperties 说明我的内部有一个属性 MvcProperties resolver.setPrefix(this.mvcProperties.getView().getPrefix()); resolver.setSuffix(this.mvcProperties.getView().getSuffix()); return resolver; } - 查看MvcProperties: @ConfigurationProperties(prefix = "spring.mvc") //说明这是一个属性批量注入的"vo"对象 public class WebMvcProperties {} - 上一步已经定义好属性的vo。那么需要再看在哪里启用了属性的批量注入 在WebMvcAutoConfiguration中有一个静态的内部类:WebMvcAutoConfigurationAdapter,定义如下: @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) public static class WebMvcAutoConfigurationAdapter{} 5. 自定义启动器 1) 准备ConfigurationProperties - com.atguigu.autoconfig.DatasourceProperties @ConfigurationProperties(prefix="com.atguigu.datasource") public class DataSourceProperties { 2) 准备DataSourceAutoConfiguration @SpringBootConfiguration @EnableAutoConfiguration(DataSourceProperties.class) public class DataSourceAutoConfiguration{ @Autowired private DataSourceProperties dataSourceProperties; @Bean public DataSource druidDataSource(){ DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(dataSourceProperties.getDriverClassName()); //... return ds; } } 3) 新建META-INF/spring.factories文件 org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.atguigu.autoconfig.DataSourceAutoConfiguration 4) 打包并安装 5) 在第二个项目中引用上一个jar @Autowired private DataSource datasource ; 6. springboot中的常用启动器 - MVC 1) 添加 starter-web 2) 静态资源目录设置 默认的是四个: 我们可以去覆盖静态资源目录 spring: resources: static-locations: classpath:/webapp/ 7. 自定义拦截器 1) 新建类实现HandlerInterceptor , 重写其中的三个方法 2) 已经没有xml文件了。我们现在需要的是配置类 因此,我们新建了一个MvcConfig @Configuration public class MvcConfig implements WebMvcConfigurer{ //重写addInterceptors方法 public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/user"); } }