springboot默認配置已經(jīng)可以使用springmvc,不過有時候我們需要進行一些自定義配置。
常見配置
server:
port: 10001 # 端口號
訪問靜態(tài)資源
我們的項目是一個jar工程,沒有webapp,那我們的靜態(tài)資源應(yīng)該放到哪呢?
在springboot入門一文中我們講到,有一個叫ResourceProperties的類,里面就定義了靜態(tài)資源的默認查找路徑。
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
省略....
}
# 靜態(tài)資源路徑
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public
例如我們在resources/static下放一張圖片


攔截器
在springboot中我們該怎么配置攔截器呢?springboot官方的文檔是這么說的。
如果你想要保持springboot的一些默認MVC特征,同時又想自定義一些MVC配置(包括:攔截器,格式化器, 視圖控制器、消息轉(zhuǎn)換器 等等),你應(yīng)該讓一個類實現(xiàn)WebMvcConfigurer,并且添加@Configuration注解,但是千萬不要加@EnableWebMvc注解。如果你想要自定義HandlerMapping、HandlerAdapter、ExceptionResolver等組件,你可以創(chuàng)建一個WebMvcRegistrationsAdapter實例 來提供以上組件。如果你想要完全自定義SpringMVC,不保留SpringBoot提供的一切特征,你可以自己定義類并且添加@Configuration注解和@EnableWebMvc注解
總結(jié): 通過實現(xiàn)WebMvcConfigurer并添加@Configuration注解來實現(xiàn)自定義部分SpringMvc配置。
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("login pre ......");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("login ......");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("login after ......");
}
}
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Bean
public LoginInterceptor loginInterceptor(){
return new LoginInterceptor();
}
/**
* 重寫addInterceptors方法,添加自定義攔截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注冊攔截器,通過addPathPatterns來添加攔截路徑
registry.addInterceptor(this.loginInterceptor()).addPathPatterns("/**");
}
}
這樣攔截器就配置好了,每次請求接口都會先走攔截器。
整合數(shù)據(jù)庫連接池
- 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
-
HikariCP數(shù)據(jù)庫連接池
引入上面兩個依賴springboot就已經(jīng)幫我們引入了一個數(shù)據(jù)庫連接池。
HikariCP數(shù)據(jù)庫連接池
HikariCP應(yīng)該是目前速度最快的連接池了,我們看看它與c3p0的對比。
HikariCP和c3p0性能對比
# application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver # 可以省略,springboot會自動推斷
hikari:
# 一個連接idle狀態(tài)的最大時長(毫秒),超時則被釋放(retired),缺省:10分鐘
idle-timeout: 60000
maximum-pool-size: 30 # 連接池中允許的最大連接數(shù)。缺省值:10
minimum-idle: 10
- Druid連接池
alibaba出品
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.6</version>
</dependency>
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver # 可以省略,springboot會自動推斷
druid:
# 初始化連接數(shù)
initial-size: 1
# 最小空閑數(shù)
min-idle: 1
# 最大連接數(shù)
max-active: 20
# 獲取連接時測試是否可用
test-on-borrow: true
# 監(jiān)控頁面啟動
stat-view-servlet:
enabled: true

