目录
特性
相较spring, springboot不需要进行繁琐的配置文件, 内部根据引入的依赖进行默认配置, 也支持对插件进行个性化配置, 内部自带web容器
启动
引入依赖
<!-- 所有的springboot工程都需要继承这个模块 内部依赖了springboot-dependencies,约定了该springboot版本对应的其他依赖版本号-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependencies>
<!--web依赖,用于展示效果->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
引导类
@SpringBootApplication
public class MySpringBootStartApplication{
public static void main(String[] args) {
SpringApplication.run(MySpringBootStartApplication.class);
}
}
运行main方法 , springboot将把这个demo项目发布到8080端口
热部署
springboot支持热部署, 改动代码后直接发布到web容器中, 但是需要ide工具配合, 即使你配置了, 也可能没办法达到效果, 需要注意的是开启热部署后, 热部署的触发时机为自动保存时, 自动保存的时机默认为idea失去焦点时
IDEA2019,springboot2.1 .6 热部署配置 完整版
idea springboot快速搭建
需要在可以访问spring官网的情况下
依赖
spring-boot-starter-parent中对配置文件进行了约定, 如果同时存在多种格式文件, 后者中的相同内容将会覆盖前者
spring-boot-starter-parent依赖了spring-boot-dependencies, 这其中对其他依赖进行了版本锁定
引导类 了解默认配置原理
springbootapplication注解中包含其他注解, 见名知意, 其中@EnableAutoConfiguration开启自动配置为 springboot核心注解,
AutoConfigurationImportSelector中声明了对自动配置的详细描述, 获取配置项, 去重, 排除等等
getCandidateConfigurations()方法为初始化配置项, 通过断言可以看出他在这个jar的目录下找一个叫spring.factories的文件, 这个jar包路径为package org.springframework.boot.autoconfigure
这个配置文件中包含了所有默认配置的配置类
这里已一个web容器为例, 可以看出他也开启了自动配置文件
@ConditionalOnClass(ServletRequest.class) 只要引入了这个依赖, 就进行自动配置
另外@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)如果缺失括号中这个类的情况下, 就使用当前WebMvcAutoConfiguration这个类
根据ServerProperties中的前缀和属性名
在json文件中找到对应的默认值为8080
对应这个默认配置, 在自己的配置文件中已相同key重定义此值为8888, 重启springboot, demo被发布在8888端口
java类配置
可以看到springboot使用了类对java进行默认配置, 并没有使用配置文件的方式, 这是spring推荐的方式, 并为这种方式提供了很多方便的获取值的工具
例如配置一个拦截器类, 加入自己的拦截器, 需要注意配置类需要符合引导类默认的包扫描规定, 既同级及子孙级目录
import com.example.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMVCConfiguration implements WebMvcConfigurer {
@Autowired
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor).addPathPatterns("/**");
}
}
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterceptor implements HandlerInterceptor {
/**
* 前置方法
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("前置执行了");
return true;
}
/**
* 后置
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("后置执行了");
}
/**
* 完成
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("完成方法执行了");
}
}
读取配置文件方式
对java的配置类的值, 为了解耦依然会放到配置文件中, springboot提供了几种获取值的方式
@PropertySource
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfiguration {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
@ConfigurationProperties
@ConfigurationProperties(prefix = “article”)根据前缀读取默认配置文件中的值
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "article")
public class ArticleProperties {
private String name;
private String title;
private String content;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
配置类
@EnableConfigurationProperties(ArticleProperties.class)开启注解,并指定java类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(ArticleProperties.class)
public class ArticleConfiguration {
@Autowired
private ArticleProperties articleProperties;
@Bean
public ArticleProperties articleProperties(){
return articleProperties;
}
}