一,连接池的配置
在pom.xml中添加,druid的maven信息
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
数据源设置:
要在init-method设置init方法,才能在监控页面中查看数据源等操作
<!--使用druid数据源-->
<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"/>
<!--配置数据源监控的filter-->
<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配置
<!-- druid的监控配置 -->
<servlet>
<servlet-name>StatViewServlet</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
<!--登陆名-->
<init-param>
<param-name>loginUsername</param-name>
<param-value>admin</param-value>
</init-param>
<!-- 登录密码-->
<init-param>
<param-name>loginPassword</param-name>
<param-value>root</param-value>
</init-param>
<!--白名单-->
<init-param>
<param-name>allow</param-name>
<param-value></param-value>
</init-param>
<!--黑名单-->
<init-param>
<param-name>deny</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>StatViewServlet</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping> <filter>
<filter-name>WebStatFilter</filter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
<!--过滤的样式-->
<init-param>
<param-name>exclusions</param-name>
<param-value>*.js,*.gif,*.css,*.ico,*.jpg,*.png,/druid/*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WebStatFilter</filter-name>
<servlet-name>StatViewServlet</servlet-name>
</filter-mapping>
<!--druid监控配置结束-->
配置完成访问项目路径下的 “/druid/就能进入监控页面,登入用户名和密码就是在servlet中配置的