一,連接池的配置 在pom.xml中添加,druid的maven信息 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> ...
一,連接池的配置
在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中配置的