文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 一、什么是springboot?
- 二、springboot特点
- 三、创建springboot工程
- 1.使用idea快速创建
- 2.pom.xml
- 四、springboot的配置文件
- 1.properties属性文件
- 2.yml属性文件
- 五、读取springboot配置文件中的内容
- 五、profiles文件的介绍
- 六、Springboot注册web三大组件
- 1.注册servlet
- 2.注册filter
一、什么是springboot?
springboot可以帮你简化spring的搭建,并且快速创建一个spring的应用程序。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置
二、springboot特点(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可 以创建可执行的JARs和WARs;
(2)内嵌Tomcat或Jetty等Servlet容器;
(3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
(4)尽可能自动配置Spring容器;
(5)提供准备好的特性,如指标、健康检查和外部化配置;
(6)绝对没有代码生成,不需要XML配置。
代码如下(示例):
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.xdl springboot 0.0.1-SNAPSHOT springboot springboot 1.8 com.spring4all swagger-spring-boot-starter 1.9.1.RELEASE com.github.xiaoymin swagger-bootstrap-ui 1.7.8 org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 com.github.pagehelper pagehelper-spring-boot-starter 1.4.2 com.alibaba druid-spring-boot-starter 1.2.8 org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
引入相关启动依赖
四、springboot的配置文件 1.properties属性文件
# 配置端口号 server.port=8080 # 配置默认路径 server.servlet.context-path=/xdl2.yml属性文件
# 配置端口号以及默认访问路径
server:
port: 8082
# 配置默认路径
servlet:
context-path: /xdl
五、读取springboot配置文件中的内容
1.OSS文件上传:
密钥和bucket名称等—密钥和bucket都写死在java代码中。如果后期修改密钥和bucket的值,你必须修改源码代码。 我们要写在配置文件。然后通过代码在读取配置文件中的密钥和bucket.
2.读取springboot配置文件的内容:
通过@PropertiesConfiguration或者@Value注解。
@PropertiesConfiguration该注解使用在类上。
#自定义的配置信息 student.name=ldh student.age=15 student.hobby[0]=sing student.hobby[1]=swing
student是自己创建的类;name是属性;=后面是自己赋得值
@Data
//该类对象的创建和销毁都有spring容器来管理
@Component
//读取springboot中的配置内容
@ConfigurationProperties(prefix = "student")
public class Student {
String name;
Integer age;
String[] hobby;
Map map;
}
控制层:
@Autowired //spring容器帮你注入该对象
private Student student;
@GetMapping("/index")
public Student index(){
return student;
}
注意:@Value 只能放在我们的类属性上。而且它只能读取基本类型和字符串类型。
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘people.map’ in value “${student.map}”
#自定义的配置信息 student.name=ldh student.ages=15 student.hobby[0]=sing student.hobby[1]=swing student.map.clazz=qy151 student.map.stuno=110五、profiles文件的介绍 六、Springboot注册web三大组件
web的三大组件:Servlet,Filter,以及Linstener监听器。
springboot有可能要集成第三方框架,而第三方框架的底层可能就依赖于过滤器或者servlet.
早期注册到Tomcat容器web.xml
Servlet类 /
而现在没有web.xml,需要建立一个配置类:
@Configuration //该类为配置类 xml文件
public class MyConfig {
@Bean //理解为配置文件中
public ServletRegistrationBean registrationBean(){
//创建一个Servlet注册器.
ServletRegistrationBean registrationBean=new ServletRegistrationBean<>();
registrationBean.setName("my");
registrationBean.setServlet(new MyServlet());
registrationBean.addUrlMappings("/my");
return registrationBean;
}
}
2.注册filter
同上,在配置类中加入FilterRegistrationBean对象
@Configuration //相当于web.xml文件
public class Config {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean<>();
registrationBean.setName("myFilter");
registrationBean.setFilter(new MyFilter());
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}



