一,连接池的配置
在pom.xml中添加,druid的maven信息
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.21version>
dependency>
数据源设置:
要在init-method设置init方法,才能在监控页面中查看数据源等操作
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="5">property>
<property name="maxActive" value="10">property>
<property name="maxWait" value="5000"/>
<property name="filters" value="stat">property>
bean>
二,数据源监控的配置:
druid数据源监控是通过其support
包下的StatViewServlet
来提供的,所以要在web.xml
进行servlet的配置
StatViewServlet的部分代码如下:
public class StatViewServlet extends ResourceServlet {
private static final Log LOG = LogFactory.getLog(StatViewServlet.class);
private static final long serialVersionUID = 1L;
public static final String PARAM_NAME_RESET_ENABLE = "resetEnable";
public static final String PARAM_NAME_JMX_URL = "jmxUrl";
public static final String PARAM_NAME_JMX_USERNAME = "jmxUsername";
public static final String PARAM_NAME_JMX_PASSWORD = "jmxPassword";
private DruidStatService statService = DruidStatService.getInstance();
private String jmxUrl = null;
private String jmxUsername = null;
private String jmxPassword = null;
private MBeanServerConnection conn = null;
public StatViewServlet() {
super("support/http/resources");
}
StatViewServlet
其资源路径在
"support/http/resources" 主要是一些静态的前端资源文件,所以要在filter中过滤
web.xml配置
<servlet>
<servlet-name>StatViewServletservlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
<init-param>
<param-name>loginUsernameparam-name>
<param-value>adminparam-value>
init-param>
<init-param>
<param-name>loginPasswordparam-name>
<param-value>rootparam-value>
init-param>
<init-param>
<param-name>allowparam-name>
<param-value>param-value>
init-param>
<init-param>
<param-name>denyparam-name>
<param-value>param-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>StatViewServletservlet-name>
<url-pattern>/druid/*url-pattern>
servlet-mapping>
<filter>
<filter-name>WebStatFilterfilter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
<init-param>
<param-name>exclusionsparam-name>
<param-value>*.js,*.gif,*.css,*.ico,*.jpg,*.png,/druid/*param-value>
init-param>
filter>
<filter-mapping>
<filter-name>WebStatFilterfilter-name>
<servlet-name>StatViewServletservlet-name>
filter-mapping>
配置完成访问项目路径下的 “/druid/就能进入监控页面,登入用户名和密码就是在servlet中配置的