1.什么是sprin boot
springboot是spring框架的一个扩展,继承了spring框架原有的特性,还通过简化配置来进一步简化了项目搭建和开发过程,另外springboot集成了大量的框架使得依赖包的版本冲突得到了很好的解决。
2.为什么使用springboot
自动配置:springboot可以自动配置应用程序所需的各种组件,比如数据库链接、web服务器等,开发人员只需要通过一些配置属性即可使用这些组件
起步依赖:springboot提供了一系列依赖库,自动配置的starter,简化Maven配置
内嵌Web服务器:springboot支持嵌入式的web服务器,如Tomcat、jetty和Undertow
无需xml配置:springboot推荐使用java的配置,而不是xml配置文件
生产就绪特性:提供准备好的特性,如指标、健康检查和外部化配置
3.如何使用springboot
1.搭建工程
选择springboot版本,因为我jdk选择了1.8所以我的springboot版本不能高于3.0
创建HelloController:
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/springboot")
public String hello(){
return "Hello SpringBoot";
}
}
运行启动类:
@SpringBootApplication
public class SpringbootDemo01Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemo01Application.class, args);
}
}
也可以创建Maven项目来添加springboot的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
</parent>
<groupId>com.gjc</groupId>
<artifactId>springboot_demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
使用maven来创建springboot需要手动添加springboot启动类
4.读取springboot中的配置的内容
1.springboot配置文件的种类
springboo提供了两种格式的配置文件,格式以及优先级不同
第一种:属性文件 application.properties
第二种:yaml文件 application.yml
优先级如果有相同配置properties>yml
在properties中设置端口号为8082
#设置端口号为8082
server.port=8082
在yml中设置端口号为8081
#设置端口号为8081
server:
port: 8081
最后运行端口号为8082
2.读取配置文件的内容
第一种通过@Value获取
在application.properties中添加
student.name=gjc
student.age=20
通过@Value获取
@Value("${student.name}")
public String name;
@Value("${student.age}")
public Integer age;
@GetMapping("/getValue")
public String getValue(){
return "name="+name+";age="+age;
}
测试:
但是这种方法只能获取基本数据类型以及字符串类型
第二种方法:@ConfigurationProperties可以封装为一个实体类,把读取的内容放入实体类中
在配置文件中添加
student.name=gjc
student.age=20
student.hobby[0]=c
student.hobby[1]=t
student.hobby[2]=r
student.hobby[3]=l
使用@ConfigurationProperties
@Component
@Data
@ConfigurationProperties("student")
public class Student {
private String name;
private Integer age;
private String[] hobby;
}
@Autowired
private Student student;
@GetMapping("/getProperties")
public Student getProperties(){
return student;
}
测试:
5.springboot如何注册web驱动
注册自定义的Servlet
1.创建一个Servlet并重写doGet()和doPost()方法
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行了doGet方法");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行力doPost方法");
}
}
2.把自定义的Servlet注册到tomcat中
@Configuration//标志为配置类 等价于配置文件
public class MyConfiguration {
@Bean //等价于配置文件中的<bean> 把方法返回的对象交给spring管理了
public ServletRegistrationBean<Servlet> registrationBean(){
ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>();
registrationBean.setServlet(new MyServlet());//相当于<servlet-class>com.gjc.servlet.MyServlet</servlet-class>
registrationBean.setName("ser");//<servlet-name>ser</servlet-name>
registrationBean.addUrlMappings("/ser");//<url-pattern>/ser</url-pattern>
return registrationBean;
}
}
测试:访问ser之后控制台执行了doGet方法
1注册自定义的Filter
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("被过滤器拦截");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
}
2.把自定义过滤器注册到内置tomcat中
@Bean
public FilterRegistrationBean<Filter> filterRegistrationBean(){
FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(new MyFilter());//<filter-class>com.gjc.filter.MyFilter</filter-class>
filterFilterRegistrationBean.setName("myFilter");//<filter-name>myFilter</filter-name>
filterFilterRegistrationBean.addUrlPatterns("/*");//拦截所有 <url-pattern>/*</url-pattern>
return filterFilterRegistrationBean;
}
测试:
被过滤器拦截
执行了doGet方法
6.springboot自动扫描的原理
在启动类上有一个@SpringBootApplication注解,相当于以下三个注解的组合:@Configuration、@EnbleAutoConfiguration和@CpmponentScan。
由@EnbleAutoConfiguration开启自动配置,会自动加载各种与classpath相关的自动配置类
由@ComponentScan来扫描特定包下的组件,注册为spring上下文中的bean,默认情况下为@SpringBootApplication主启动类所在的包以及其子包下
所以我们可以使用@ComponentScan()来指定包扫描路径
@SpringBootApplication
@ComponentScan(basePackages = "com.gjc.controller")//指定springboot扫描包
public class SpringbootDemo01Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemo01Application.class, args);
}
}