package com.clouds.util;
import java.io.File;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
/**
* @author Wang Huifeng
*
*/
public class ProxoolListener implements ServletContextListener {
private static final Log log = LogFactory.getLog(ProxoolListener.class);
private static final String XML_FILE_PROPERTY = "xmlFile";
private static final String PROPERTY_FILE_PROPERTY = "propertyFile";
private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";
private boolean autoShutdown = true;
public void contextInitialized(ServletContextEvent contextEvent) {
ServletContext context = contextEvent.getServletContext();
String appDir = contextEvent.getServletContext().getRealPath("/");
Properties properties = new Properties();
Enumeration<?> names = context.getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = context.getInitParameter(name);
if (name.equals(XML_FILE_PROPERTY)) {
try {
File file = new File(value);
if (file.isAbsolute()) {
JAXPConfigurator.configure(value, false);
} else {
JAXPConfigurator.configure(appDir + File.separator
+ value, false);
}
} catch (ProxoolException e) {
log.error("Problem configuring " + value, e);
}
} else if (name.equals(PROPERTY_FILE_PROPERTY)) {
try {
File file = new File(value);
if (file.isAbsolute()) {
PropertyConfigurator.configure(value);
} else {
PropertyConfigurator.configure(appDir + File.separator
+ value);
}
} catch (ProxoolException e) {
log.error("Problem configuring " + value, e);
}
} else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {
autoShutdown = Boolean.valueOf(value).booleanValue();
} else if (name.startsWith("jdbc")) {
properties.setProperty(name, value);
}
}
if (properties.size() > 0) {
try {
PropertyConfigurator.configure(properties);
} catch (ProxoolException e) {
log.error("Problem configuring using init properties", e);
}
}
}
public void contextDestroyed(ServletContextEvent arg0) {
if (this.autoShutdown) {
ProxoolFacade.shutdown(0);
}
}
}
proxool的配置文件如下:
## proxool configuration
jdbc-0.proxool.alias=datasource
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.proxool.driver-url=jdbc\:mysql\://192.168.2.109\:3310/demo2_edit?useUnicode\=true&characterEncoding\=utf-8&zeroDateTimeBehavior\\\=convertToNull
jdbc-0.user=root
jdbc-0.password=123456
jdbc-0.proxool.minimum-connection-count=2
jdbc-0.proxool.maximum-connection-count=10
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
jdbc-0.proxool.statistics=1m,15m,1d
jdbc-0.proxool.house-keeping-sleep-time=6000
jdbc-0.proxool.simultaneous-build-throttle=20
jdbc-0.proxool.prototype-count=3
jdbc-0.proxool.trace=true
在web.xml中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>/logon.jsp</welcome-file>
</welcome-file-list>
<!-- 读取proxool.properties配置文件 -->
<context-param>
<param-name>propertyFile</param-name>
<param-value>WEB-INF/classes/proxool.properties</param-value>
</context-param>
<!-- 读取spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext*.xml
</param-value>
</context-param>
<!-- log4J日志-->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>businessMg.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- log4j Listener,log4j的监视线程,并每(log4jRefreshInterval变量定义)秒检测日志配置变化,从而不需要每次重新启动web服务来应用新的配置。-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- proxool监听 -->
<listener>
<listener-class>com.clouds.util.ProxoolListener</listener-class>
</listener>
<!-- spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--使用proxool查看数据库的运行状态-->
<servlet>
<servlet-name>proxool</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>proxool</servlet-name>
<url-pattern>/proxool</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>XhEditor</servlet-name>
<servlet-class>com.clouds.util.UploadFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XhEditor</servlet-name>
<url-pattern>/servlet/UploadFileServlet</url-pattern>
</servlet-mapping>
<!-- struts2配置文件 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--
session监听 <listener> <listener-class> com.clouds.util.MyListener
</listener-class> </listener>
-->
<!-- dwr配置开始 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>classes</param-name>
<param-value>java.lang.Object</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- dwr配置结束 -->
</web-app>
Spring中的proxool的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.clouds" />
<!-- 支持 @AspectJ 标记
<aop:aspectj-autoproxy />
<bean class="com.clouds.util.LogAspect" />
-->
<!-- Proxool 数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.datasource</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<!-- 扫描带注解的实体类 -->
<property name="packagesToScan">
<list>
<value>com.clouds.pojo</value>
</list>
</property>
<!-- 提供注解扫描类的设置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!--
<prop key="hibernate.show_sql">true</prop> \
-->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
</bean>
<!-- hibernate属性配置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置spring jdbc事务操作模板里面会有一些增删改查的操作,跟HibernateTemplate,JpaTemplate是一个意思 ,特点不一而已-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="orderDao" class="com.clouds.order.dao.impl.orderDaoImpl" scope="singleton">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="biddingDao" class="com.clouds.bidding.dao.impl.BiddingDaoImpl" scope="singleton">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="cpsDao" class="com.clouds.cps.dao.impl.CpsDaoImpl" scope="singleton">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="tagDao" class="com.clouds.util.tag.dao.impl.tagDaoImpl" scope="singleton">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="deliveryTag" class="com.clouds.util.tag.DeliveryTag" scope="singleton">
<property name="tagDao" ref="tagDao"></property>
</bean>
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--
注解事务-->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(public * com.clouds.service.*.*.*(..))"
id="service-pc" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="service-pc" />
</aop:config>
-->
</beans>