SpringBoot整合Druid
POM.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
application.yml
frame:
druid:
allow:
loginusername: admin
loginpassword: 11111
resetenable: false
deny:
urlmappings: /druid/*
Druid配置文件映射实体
package com.fff.vo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "frame.druid", ignoreInvalidFields = true)
public class DruidConfigVo {
private String allow;
private String loginusername;
private String loginpassword;
private String deny;
private String resetenable;
private String urlmappings;
public String getAllow() {
return allow;
}
public void setAllow(String allow) {
this.allow = allow;
}
public String getLoginusername() {
return loginusername;
}
public void setLoginusername(String loginusername) {
this.loginusername = loginusername;
}
public String getLoginpassword() {
return loginpassword;
}
public void setLoginpassword(String loginpassword) {
this.loginpassword = loginpassword;
}
public String getDeny() {
return deny;
}
public void setDeny(String deny) {
this.deny = deny;
}
public String getResetenable() {
return resetenable;
}
public void setResetenable(String resetenable) {
this.resetenable = resetenable;
}
public String getUrlmappings() {
return urlmappings;
}
public void setUrlmappings(String urlmappings) {
this.urlmappings = urlmappings;
}
}
根据配置初始化Druid配置
package com.fff.config;
import com.alibaba.druid.support.http.StatViewServlet;
import com.fff.vo.DruidConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
@Autowired
private DruidConfigVo druidConfigVo;
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new StatViewServlet());
servletRegistrationBean.addUrlMappings(druidConfigVo.getUrlmappings());
Map<String, String> initParameters = new HashMap<>();
initParameters.put("resetEnable", druidConfigVo.getResetenable());
initParameters.put("allow", druidConfigVo.getAllow());
initParameters.put("loginUsername", druidConfigVo.getLoginusername());
initParameters.put("loginPassword", druidConfigVo.getLoginpassword());
initParameters.put("deny", druidConfigVo.getDeny());
servletRegistrationBean.setInitParameters(initParameters);
return servletRegistrationBean;
}
}