springboot默認(rèn)配置已經(jīng)可以使用springmvc,不過(guò)有時(shí)候我們需要進(jìn)行一些自定義配置。
常見(jiàn)配置
server:
port: 10001 # 端口號(hào)
訪問(wèn)靜態(tài)資源
我們的項(xiàng)目是一個(gè)jar工程,沒(méi)有webapp,那我們的靜態(tài)資源應(yīng)該放到哪呢?
在springboot入門一文中我們講到,有一個(gè)叫ResourceProperties的類,里面就定義了靜態(tài)資源的默認(rèn)查找路徑。
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
例如我們?cè)趓esources/static下放一張圖片
攔截器
在springboot中我們?cè)撛趺磁渲脭r截器呢?springboot官方的文檔是這么說(shuō)的。
如果你想要保持springboot的一些默認(rèn)MVC特征,同時(shí)又想自定義一些MVC配置(包括:攔截器,格式化器, 視圖控制器、消息轉(zhuǎn)換器 等等),你應(yīng)該讓一個(gè)類實(shí)現(xiàn)WebMvcConfigurer,并且添加@Configuration注解,但是千萬(wàn)不要加@EnableWebMvc注解。如果你想要自定義HandlerMapping、HandlerAdapter、ExceptionResolver等組件,你可以創(chuàng)建一個(gè)WebMvcRegistrationsAdapter實(shí)例 來(lái)提供以上組件。如果你想要完全自定義SpringMVC,不保留SpringBoot提供的一切特征,你可以自己定義類并且添加@Configuration注解和@EnableWebMvc注解
總結(jié): 通過(guò)實(shí)現(xiàn)WebMvcConfigurer并添加@Configuration注解來(lái)實(shí)現(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) {
//注冊(cè)攔截器,通過(guò)addPathPatterns來(lái)添加攔截路徑
registry.addInterceptor(this.loginInterceptor()).addPathPatterns("/**");
}
}
這樣攔截器就配置好了,每次請(qǐng)求接口都會(huì)先走攔截器。
整合數(shù)據(jù)庫(kù)連接池
- 依賴
<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ù)庫(kù)連接池
引入上面兩個(gè)依賴springboot就已經(jīng)幫我們引入了一個(gè)數(shù)據(jù)庫(kù)連接池。
HikariCP數(shù)據(jù)庫(kù)連接池
HikariCP應(yīng)該是目前速度最快的連接池了,我們看看它與c3p0的對(duì)比。
HikariCP和c3p0性能對(duì)比
# application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver # 可以省略,springboot會(huì)自動(dòng)推斷
hikari:
# 一個(gè)連接idle狀態(tài)的最大時(shí)長(zhǎng)(毫秒),超時(shí)則被釋放(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會(huì)自動(dòng)推斷
druid:
# 初始化連接數(shù)
initial-size: 1
# 最小空閑數(shù)
min-idle: 1
# 最大連接數(shù)
max-active: 20
# 獲取連接時(shí)測(cè)試是否可用
test-on-borrow: true
# 監(jiān)控頁(yè)面啟動(dòng)
stat-view-servlet:
enabled: true