开始
相信很多朋友在使用新版本的Spring的时候,发现了一些叫jakarta的包,看起来有点陌生。
很多时候,比较纠结不知道该导入哪一个包。
jakarta其实就是之前的javax。
主要JavaEE相关的,从之前javax名字也可以看出来,主要是扩展部分,最典型的就是Servlet。
Oracle已经将JavaEE捐献给了Eclipse基金会,从JavaEE 8开始就被正式被改名为jakarta EE
jakarta EE官网
eclipse Jakarta组
javax这个包是Oracle的,涉及商标和版权原因,所以改名也很正常。
新版本的除了包含在JDK包中的除了javax.sql部分的,其他的javax部分基本都会替换为jakarta。
<dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>6.1.0-M1</version><scope>provided</scope>
</dependency>
javax.servlet在2018年之后就没有更新了,因为2018年Oracle把JavaEE捐赠给Eclipse基金会之后项目就迁移了。
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope>
</dependency>
新版本的Spring已经使用的是Jakarta了:
Druid也添加了相应的支持:
下面我们就以Druid为例,来看一看。
Druid starter方式
可以使用druid的starter
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.20</version>
</dependency>
spring.datasource.initial-size=5
spring.datasource.min-idle=5
spring.datasource.max-active=25
spring.datasource.max-wait=1000
spring.datasource.time-between-eviction-runs-millis=60000
spring.datasource.validation-query=SELECT 1 as nums
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
spring.datasource.pool-prepared-statements=true
spring.datasource.max-pool-prepared-statement-per-connection-size=20spring.datasource.druid.filters=stat,wall,slf4jspring.druid.stat-view-servlet.enabled=true
spring.druid.stat-view-servlet.url-pattern=/druid/*
spring.druid.stat-view-servlet.login-username=tim
spring.druid.stat-view-servlet.login-password=123456spring.druid.web-stat-filter.enabled=true
spring.druid.web-stat-filter.url-pattern=/*
spring.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'spring.druid.filter.stat.slow-sql-millis=1000
spring.druid.filter.stat.log-slow-sql=true
spring.druid.filter.stat.enabled=truespring.druid.filter.wall.enabled=true
spring.druid.filter.wall.config.drop-table-allow=false
spring.druid.filter.wall.config.select-all-column-allow=false
自定义Config
也可以直接使用druid
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.20</version>
</dependency>
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;@Configuration
@Slf4j
public class DruidDataSourceConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource dataSource() {DruidDataSource druidDataSource = new DruidDataSource();try {druidDataSource.setFilters("stat,wall");} catch (SQLException e) {log.error(e.getMessage(),e);}return druidDataSource;}@Beanpublic ServletRegistrationBean<StatViewServlet> statViewServlet() {StatViewServlet statViewServlet = new StatViewServlet();ServletRegistrationBean<StatViewServlet> servletRegistrationBean =new ServletRegistrationBean<>(statViewServlet, "/druid/*");servletRegistrationBean.addInitParameter("loginUsername", "druid");servletRegistrationBean.addInitParameter("loginPassword", "123456");return servletRegistrationBean;}@Beanpublic FilterRegistrationBean<WebStatFilter> webStatFilter() {WebStatFilter webStatFilter = new WebStatFilter();FilterRegistrationBean<WebStatFilter> filterRegistrationBean =new FilterRegistrationBean<>(webStatFilter);// 设置统计的Web URLfilterRegistrationBean.setUrlPatterns(List.of("/*"));// 设置排除的Web URLfilterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}
}
druid github