时间:2021-07-01 10:21:17 帮助过:6人阅读
2、web.xml
使用druid监控主要配置DruidWebStatFilter、StatViewServlet、druid用户名和密码(可选)
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--告知javaEE容器,有那些内容需要添加到上下文里去--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--log4j参数配置--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:config/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!--spring 前端控制器--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--druid监控servlet--> <servlet> <servlet-name>druidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <!-- 允许清空统计数据 --> <param-name>resetEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- 用户名 --> <param-name>loginUsername</param-name> <param-value>sqlserver</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginPassword</param-name> <param-value>123</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>druidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!--防止js,css等文件被springmvc拦截 让tomcat来处理--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/2sc/js/*</url-pattern> <url-pattern>/2sc/Style/*</url-pattern> </servlet-mapping> </web-app>
3、springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd 12 "> 13 14 <!--从配置文件加载数据库信息--> 15 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 16 <property name="locations" value="classpath:config/jdbc.properties"/> 17 <property name="fileEncoding" value="UTF-8"/> 18 </bean> 19 20 <!--配置数据源,这里使用Spring默认--> 21 <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">--> 22 <!--<property name="driverClassName" value="${sqlserver.driver}"/>--> 23 <!--<property name="url" value="${sqlserver.url}"/>--> 24 <!--<property name="username" value="${sqlserver.username}"/>--> 25 <!--<property name="password" value="${sqlserver.password}"/>--> 26 <!--</bean>--> 27 28 <!--配置数据源 druid--> 29 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 30 <property name="driverClassName" value="${sqlserver.driver}"/> 31 <property name="url" value="${sqlserver.url}" /> 32 <property name="username" value="${sqlserver.username}" /> 33 <property name="password" value="${sqlserver.password}" /> 34 35 <property name="filters" value="stat" /> 36 37 <property name="maxActive" value="20" /> 38 <property name="initialSize" value="1" /> 39 <property name="maxWait" value="60000" /> 40 <property name="minIdle" value="1" /> 41 42 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 43 <property name="minEvictableIdleTimeMillis" value="300000" /> 44 45 <property name="testWhileIdle" value="true" /> 46 <property name="testOnBorrow" value="false" /> 47 <property name="testOnReturn" value="false" /> 48 49 <property name="poolPreparedStatements" value="true" /> 50 <property name="maxOpenPreparedStatements" value="20" /> 51 </bean> 52 53 <!--扫描Mapper--> 54 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 55 <property name="basePackage" value="com.autohome.mapper"/> 56 </bean> 57 58 <!--配置sqlSessionFactory--> 59 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 60 <property name="configLocation" value="classpath:springmvc-mybatis.xml"/> 61 <property name="dataSource" ref="dataSource"/> 62 </bean> 63 64 <!--启用最新的注解器、映射器--> 65 <mvc:annotation-driven/> 66 67 <!--使用默认的Servlet来响应静态资源--> 68 <mvc:default-servlet-handler/> 69 70 <!--扫描Controller注解类--> 71 <context:component-scan base-package="com.autohome.controller" /> 72 <!--扫描Service注解类--> 73 <context:component-scan base-package="com.autohome.service"/> 74 75 <!--velocity模板配置--> 76 <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 77 <property name="resourceLoaderPath" value="/WEB-INF/views/"/> 78 <property name="configLocation" value="classpath:config/velocity.properties"/> 79 <property name="velocityProperties"> 80 <props> 81 <prop key="input.encoding">UTF-8</prop> 82 <prop key="output.encoding">UTF-8</prop> 83 </props> 84 </property> 85 </bean> 86 87 <!--配置视图解析器--> 88 89 <!--jsp视图解析器--> 90 <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">--> 91 <!--<property name="prefix" value="/WEB-INF/views/"/>--> 92 <!--<property name="suffix" value=".jsp"/>--> 93 <!--</bean>--> 94 95 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 96 <property name="suffix" value=".vm"/> 97 <property name="prefix" value=""/> 98 <property name="contentType" value="text/html;charset=UTF-8"/> 99 </bean> 100 101 </beans>
项目结构我在第一篇ssm整合的基础上继续开发,加入了log4j、druid监控。
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE
SpringMVC4+MyBatis+SQL Server2014+druid 监控SQL运行情况
标签:扫描 als sys erp eve location git time pat